You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
经典DP问题:
可以参考http://www.csie.ntnu.edu.tw/~u91029/DynamicProgramming.html#1
图文并茂
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n < 0:
return -1# prevent invalid input: negative integer
dp = [0,1,2]
for i in range(3, n+1):
dp.append(dp[i-1] + dp[i-2])
return dp[n]