Problem Description
Suppose you are climbing stairs. Need n order to reach your roof.
Every time you can climb one or two steps. How many different ways can climb to the roof of it?
Note: Given n is a positive integer.
Examples
Example 1:
Input: 2
Output: 2
explanation: There are two methods can climb the roof.
- 1 + 1-order-order
- Step 2
Example 2:
Input: 3
Output: 3
Explanation: There are three methods can climb to the roof.
- Step 1 + 1 + 1-order-order
- 2 + 1-order-order
- 2 + 1-order-order
achieve
Dynamic Programming
When n is greater than or equal to 2, the method reaches step number of types of n-th order of f (n-1) + f (n-2)
def climb_stair(n):
"""
动态规划方法,easy级别
"""
# 开数组
step = list(0 for _ in range(n+1))
step[1] = 1
step[2] = 2
if n == 1:
return 1
for i in range(3, n+1):
step[i] = step[i-1] + step[i-2]
return step[n]
Time complexity of O (n)
Space complexity O (n)