[!CODE]- Given n stairs, find the no. if ways a person can climb them if he can only climb 1 or two stairs at once
def stair(n):
if n==1 or n==0:
return 1
else:
return (stair(n-1)+stair(n-2))Python Solution for Climbing Stairs
def stair(n): if n==1 or n==0: return 1 else: return (stair(n-1)+stair(n-2))
better algorithm design:-
def two_pow(n):
if (n==0):
return 1
else:
half = two_pow(n//2)
if (n%2 == 0):
return (half*half)
else:
return (2*half*half)
print(two_pow(3))this is better than doing 2*2^n-1 recursion as that will take n cyclles this will take log(n) cycles