File tree Expand file tree Collapse file tree 1 file changed +3
-2
lines changed Expand file tree Collapse file tree 1 file changed +3
-2
lines changed Original file line number Diff line number Diff line change 7
7
```
8
8
You are climbing a stair case. It takes n steps to reach to the top.
9
9
10
- Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
10
+ Each time you can either climb 1 or 2 steps.
11
+ In how many distinct ways can you climb to the top?
11
12
12
13
Example
13
14
Given an example n=3 , 1+1+1=2+1=1+2=3
@@ -24,7 +25,7 @@ return 3
24
25
3 . Initialization: f[ 0] =1,f[ 1] =1
25
26
4 . Answer: f[ n]
26
27
27
- 尤其注意状态转移方程的写法,f[ i] 只可能由两个中间状态转化而来,一个是f[ i-1] ,由f[ i-1] 到f[ i] 其方法总数并未增加;另一个是f[ i-2] ,由f[ i-2] 到f[ i] 隔了两个台阶,因此有1+1和2两个方法,因此容易写成 f[ i] =f[ i-1] +f[ i-2] +1,但仔细分析后能发现,由f[ i-2] 到f[ i] 的中间状态f[ i-1] 已经被利用过一次,故f[ i] =f[ i-1] +f[ i-2]
28
+ 尤其注意状态转移方程的写法,f[ i] 只可能由两个中间状态转化而来,一个是f[ i-1] ,由f[ i-1] 到f[ i] 其方法总数并未增加;另一个是f[ i-2] ,由f[ i-2] 到f[ i] 隔了两个台阶,因此有1+1和2两个方法,因此容易写成 f[ i] =f[ i-1] +f[ i-2] +1,但仔细分析后能发现,由f[ i-2] 到f[ i] 的中间状态f[ i-1] 已经被利用过一次,故f[ i] =f[ i-1] +f[ i-2] . 使用动规思想解题时需要分清『重叠子状态』, 如果有重复的需要去重。
28
29
29
30
### C++
30
31
You can’t perform that action at this time.
0 commit comments