mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 11:05:36 +00:00
Update 10.4 变态跳台阶.md
补充动态规划解法的转移方程推导与复杂度说明,更新数学推导中递推式适用条件及通项公式计算说明。
This commit is contained in:
+3
-1
@@ -10,6 +10,8 @@
|
||||
|
||||
### 动态规划
|
||||
|
||||
dp[i] 表示跳上第 i + 1 级台阶的跳法数。最后一跳可以从地面直接跳上来(1 种),也可以从下面任意一级 j 跳上来(dp[j] 种),因此 dp[i] = 1 + dp[0] + dp[1] + …… + dp[i-1],代码中先把每个 dp[i] 初始化为 1,再累加。时间复杂度 O(N<sup>2</sup>)。
|
||||
|
||||
```java
|
||||
public int jumpFloorII(int target) {
|
||||
int[] dp = new int[target];
|
||||
@@ -47,7 +49,7 @@ f(n) - f(n-1) = f(n-1)
|
||||
f(n) = 2*f(n-1)
|
||||
```
|
||||
|
||||
所以 f(n) 是一个等比数列
|
||||
第一步的展开要求 n - 1 ≥ 1,所以上式在 n ≥ 2 时成立;又 f(1) = 1,因此 f(n) = 2<sup>n-1</sup>,例如 f(4) = 8。
|
||||
|
||||
```java
|
||||
public int JumpFloorII(int target) {
|
||||
|
||||
Reference in New Issue
Block a user