Climbing Stairs
Question
Given a positive integer n, calculate the number of distinct ways to climb a staircase of n steps.
Example 1
Input: n = 3
Output: 3
Explanation: There are three ways to climb the stairs:
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Solution
- ▭
- ▯
all//Climbing Stairs.py
def climbStairs(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return climbStairs(n-1) + climbStairs(n-2)
# Driver program
n = 5
print(climbStairs(n))
all//Climbing Stairs.py
def climbStairs(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return climbStairs(n-1) + climbStairs(n-2)
# Driver program
n = 5
print(climbStairs(n))