You is climbing a stair case. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?
Example
Given An example n=3, 1+1+1=2+1=1+2=3
Return 3
For the problem, try-to-think about it on this.
Firstly, we define the problem of DP (n) as the ways of approaching to n stairs.
The problem of DP (n) depends on DP (N-1) and DP (N-2). Then the DP (n) = DP (N-1) + DP (n-2). Because there is a possibilities for DP (n) happen due to the rule that either it's accomplished by step 1 or Step 2 STA IRS before approaching to N.
Initialize the DP (0) =1 and DP (1) = 1.
Solve problem DP (n)
1 Public classSolution {2 /**3 * @paramN:an integer4 * @return: An integer5 */6 Public intClimbstairs (intN) {7 //Write your code here8 if(N <= 1) {9 return1;Ten } One intLast = 1, lastlast = 1; A intnow = 0; - for(inti = 1; I < n; i++) { -now = last +Lastlast; theLastlast =Last ; -Last =Now ; - } - returnNow ; + } -}
Then this problem is a Fibonacci sequence
Lintcode Climbing Stairs