Problem
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?
Note: Given $n$ will be a positive integer.
Example 1:
1 |
|
Example 2:
1 |
|
Explanation
-
We can use dynamic programming to solve this problem. The base case is when n = 0, res = 0; when n = 1, res = 1; when n = 2, res = 2.
-
The way to reach the current
i
step is equal to the way to reachi-1
step plus the way to reachi-2
step.
Solution
1 |
|