hello-algo/docs/chapter_dynamic_programming/intro_to_dynamic_programming.md

1478 lines
59 KiB
Markdown
Raw Normal View History

2023-10-06 13:31:21 +08:00
---
comments: true
---
# 14.1   初探动态规划
「动态规划 dynamic programming」是一个重要的算法范式它将一个问题分解为一系列更小的子问题并通过存储子问题的解来避免重复计算从而大幅提升时间效率。
在本节中,我们从一个经典例题入手,先给出它的暴力回溯解法,观察其中包含的重叠子问题,再逐步导出更高效的动态规划解法。
!!! question "爬楼梯"
2023-12-02 06:24:05 +08:00
给定一个共有 $n$ 阶的楼梯,你每步可以上 $1$ 阶或者 $2$ 阶,请问有多少种方案可以爬到楼顶?
2023-10-06 13:31:21 +08:00
如图 14-1 所示,对于一个 $3$ 阶楼梯,共有 $3$ 种方案可以爬到楼顶。
2023-11-09 05:13:48 +08:00
![爬到第 3 阶的方案数量](intro_to_dynamic_programming.assets/climbing_stairs_example.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 14-1 &nbsp; 爬到第 3 阶的方案数量 </p>
2023-12-02 06:24:05 +08:00
本题的目标是求解方案数量,**我们可以考虑通过回溯来穷举所有可能性**。具体来说,将爬楼梯想象为一个多轮选择的过程:从地面出发,每轮选择上 $1$ 阶或 $2$ 阶,每当到达楼梯顶部时就将方案数量加 $1$ ,当越过楼梯顶部时就将其剪枝。代码如下所示:
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="climbing_stairs_backtrack.py"
2023-10-06 14:10:18 +08:00
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:
"""回溯"""
# 当爬到第 n 阶时,方案数量加 1
if state == n:
res[0] += 1
# 遍历所有选择
for choice in choices:
# 剪枝:不允许越过第 n 阶
if state + choice > n:
2023-11-11 23:03:42 +08:00
continue
2023-10-06 14:10:18 +08:00
# 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res)
# 回退
def climbing_stairs_backtrack(n: int) -> int:
"""爬楼梯:回溯"""
2023-12-02 06:24:05 +08:00
choices = [1, 2] # 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
state = 0 # 从第 0 阶开始爬
res = [0] # 使用 res[0] 记录方案数量
backtrack(choices, state, n, res)
return res[0]
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="climbing_stairs_backtrack.cpp"
2023-10-06 14:10:18 +08:00
/* 回溯 */
void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
// 当爬到第 n 阶时,方案数量加 1
if (state == n)
res[0]++;
// 遍历所有选择
for (auto &choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
2023-11-11 23:03:42 +08:00
continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
2023-12-02 06:24:05 +08:00
vector<int> choices = {1, 2}; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
int state = 0; // 从第 0 阶开始爬
vector<int> res = {0}; // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
return res[0];
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="climbing_stairs_backtrack.java"
2023-10-06 14:10:18 +08:00
/* 回溯 */
void backtrack(List<Integer> choices, int state, int n, List<Integer> res) {
// 当爬到第 n 阶时,方案数量加 1
if (state == n)
res.set(0, res.get(0) + 1);
// 遍历所有选择
for (Integer choice : choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
2023-11-11 23:03:42 +08:00
continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
2023-12-02 06:24:05 +08:00
List<Integer> choices = Arrays.asList(1, 2); // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
int state = 0; // 从第 0 阶开始爬
List<Integer> res = new ArrayList<>();
res.add(0); // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
return res.get(0);
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="climbing_stairs_backtrack.cs"
2023-10-06 14:10:18 +08:00
/* 回溯 */
2023-10-08 01:43:28 +08:00
void Backtrack(List<int> choices, int state, int n, List<int> res) {
2023-10-06 14:10:18 +08:00
// 当爬到第 n 阶时,方案数量加 1
if (state == n)
res[0]++;
// 遍历所有选择
foreach (int choice in choices) {
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
2023-11-11 23:03:42 +08:00
continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
2023-10-08 01:43:28 +08:00
Backtrack(choices, state + choice, n, res);
2023-10-06 14:10:18 +08:00
// 回退
}
}
/* 爬楼梯:回溯 */
2023-10-08 01:43:28 +08:00
int ClimbingStairsBacktrack(int n) {
2023-12-02 06:24:05 +08:00
List<int> choices = [1, 2]; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
int state = 0; // 从第 0 阶开始爬
2023-11-27 02:32:06 +08:00
List<int> res = [0]; // 使用 res[0] 记录方案数量
2023-10-08 01:43:28 +08:00
Backtrack(choices, state, n, res);
2023-10-06 14:10:18 +08:00
return res[0];
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="climbing_stairs_backtrack.go"
2023-10-06 14:10:18 +08:00
/* 回溯 */
func backtrack(choices []int, state, n int, res []int) {
// 当爬到第 n 阶时,方案数量加 1
if state == n {
res[0] = res[0] + 1
}
// 遍历所有选择
for _, choice := range choices {
// 剪枝:不允许越过第 n 阶
if state+choice > n {
2023-11-11 23:03:42 +08:00
continue
2023-10-06 14:10:18 +08:00
}
// 尝试:做出选择,更新状态
backtrack(choices, state+choice, n, res)
// 回退
}
}
/* 爬楼梯:回溯 */
func climbingStairsBacktrack(n int) int {
2023-12-02 06:24:05 +08:00
// 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
choices := []int{1, 2}
// 从第 0 阶开始爬
state := 0
res := make([]int, 1)
// 使用 res[0] 记录方案数量
res[0] = 0
backtrack(choices, state, n, res)
return res[0]
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="climbing_stairs_backtrack.swift"
2023-10-06 14:10:18 +08:00
/* 回溯 */
func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {
// 当爬到第 n 阶时,方案数量加 1
if state == n {
res[0] += 1
}
// 遍历所有选择
for choice in choices {
// 剪枝:不允许越过第 n 阶
if state + choice > n {
2023-11-11 23:03:42 +08:00
continue
2023-10-06 14:10:18 +08:00
}
backtrack(choices: choices, state: state + choice, n: n, res: &res)
}
}
/* 爬楼梯:回溯 */
func climbingStairsBacktrack(n: Int) -> Int {
2023-12-02 06:24:05 +08:00
let choices = [1, 2] // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
let state = 0 // 从第 0 阶开始爬
var res: [Int] = []
res.append(0) // 使用 res[0] 记录方案数量
backtrack(choices: choices, state: state, n: n, res: &res)
return res[0]
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="climbing_stairs_backtrack.js"
2023-10-06 14:10:18 +08:00
/* 回溯 */
function backtrack(choices, state, n, res) {
// 当爬到第 n 阶时,方案数量加 1
if (state === n) res.set(0, res.get(0) + 1);
// 遍历所有选择
for (const choice of choices) {
// 剪枝:不允许越过第 n 阶
2023-11-11 23:03:42 +08:00
if (state + choice > n) continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
function climbingStairsBacktrack(n) {
2023-12-02 06:24:05 +08:00
const choices = [1, 2]; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
const state = 0; // 从第 0 阶开始爬
const res = new Map();
res.set(0, 0); // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
return res.get(0);
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="climbing_stairs_backtrack.ts"
2023-10-06 14:10:18 +08:00
/* 回溯 */
function backtrack(
choices: number[],
state: number,
n: number,
res: Map<0, any>
): void {
// 当爬到第 n 阶时,方案数量加 1
if (state === n) res.set(0, res.get(0) + 1);
// 遍历所有选择
for (const choice of choices) {
// 剪枝:不允许越过第 n 阶
2023-11-11 23:03:42 +08:00
if (state + choice > n) continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
function climbingStairsBacktrack(n: number): number {
2023-12-02 06:24:05 +08:00
const choices = [1, 2]; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
const state = 0; // 从第 0 阶开始爬
const res = new Map();
res.set(0, 0); // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
return res.get(0);
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="climbing_stairs_backtrack.dart"
2023-10-06 14:10:18 +08:00
/* 回溯 */
void backtrack(List<int> choices, int state, int n, List<int> res) {
// 当爬到第 n 阶时,方案数量加 1
if (state == n) {
res[0]++;
}
// 遍历所有选择
for (int choice in choices) {
// 剪枝:不允许越过第 n 阶
2023-11-11 23:03:42 +08:00
if (state + choice > n) continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
2023-12-02 06:24:05 +08:00
List<int> choices = [1, 2]; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
int state = 0; // 从第 0 阶开始爬
List<int> res = [];
res.add(0); // 使用 res[0] 记录方案数量
backtrack(choices, state, n, res);
return res[0];
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="climbing_stairs_backtrack.rs"
2023-10-06 14:10:18 +08:00
/* 回溯 */
fn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {
// 当爬到第 n 阶时,方案数量加 1
if state == n { res[0] = res[0] + 1; }
// 遍历所有选择
for &choice in choices {
// 剪枝:不允许越过第 n 阶
2023-11-11 23:03:42 +08:00
if state + choice > n { continue; }
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
/* 爬楼梯:回溯 */
fn climbing_stairs_backtrack(n: usize) -> i32 {
2023-12-02 06:24:05 +08:00
let choices = vec![ 1, 2 ]; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
let state = 0; // 从第 0 阶开始爬
let mut res = Vec::new();
res.push(0); // 使用 res[0] 记录方案数量
backtrack(&choices, state, n as i32, &mut res);
res[0]
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="climbing_stairs_backtrack.c"
2023-10-06 14:10:18 +08:00
/* 回溯 */
void backtrack(int *choices, int state, int n, int *res, int len) {
// 当爬到第 n 阶时,方案数量加 1
if (state == n)
res[0]++;
// 遍历所有选择
for (int i = 0; i < len; i++) {
int choice = choices[i];
// 剪枝:不允许越过第 n 阶
if (state + choice > n)
2023-11-11 23:03:42 +08:00
continue;
2023-10-06 14:10:18 +08:00
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res, len);
// 回退
}
}
/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
2023-12-02 06:24:05 +08:00
int choices[2] = {1, 2}; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
int state = 0; // 从第 0 阶开始爬
int *res = (int *)malloc(sizeof(int));
2023-10-18 02:16:55 +08:00
*res = 0; // 使用 res[0] 记录方案数量
2023-10-06 14:10:18 +08:00
int len = sizeof(choices) / sizeof(int);
backtrack(choices, state, n, res, len);
int result = *res;
free(res);
return result;
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="climbing_stairs_backtrack.zig"
2023-10-06 14:10:18 +08:00
// 回溯
fn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {
// 当爬到第 n 阶时,方案数量加 1
if (state == n) {
res.items[0] = res.items[0] + 1;
}
// 遍历所有选择
for (choices) |choice| {
// 剪枝:不允许越过第 n 阶
if (state + choice > n) {
2023-11-11 23:03:42 +08:00
continue;
2023-10-06 14:10:18 +08:00
}
// 尝试:做出选择,更新状态
backtrack(choices, state + choice, n, res);
// 回退
}
}
// 爬楼梯:回溯
fn climbingStairsBacktrack(n: usize) !i32 {
2023-12-02 06:24:05 +08:00
var choices = [_]i32{ 1, 2 }; // 可选择向上爬 1 阶或 2 阶
2023-10-06 14:10:18 +08:00
var state: i32 = 0; // 从第 0 阶开始爬
var res = std.ArrayList(i32).init(std.heap.page_allocator);
defer res.deinit();
try res.append(0); // 使用 res[0] 记录方案数量
backtrack(&choices, state, @intCast(n), res);
return res.items[0];
}
2023-10-06 13:31:21 +08:00
```
2024-01-07 23:42:54 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20backtrack%28choices%3A%20list%5Bint%5D,%20state%3A%20int,%20n%3A%20int,%20res%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E7%88%AC%E5%88%B0%E7%AC%AC%20n%20%E9%98%B6%E6%97%B6%EF%BC%8C%E6%96%B9%E6%A1%88%E6%95%B0%E9%87%8F%E5%8A%A0%201%0A%20%20%20%20if%20state%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%5B0%5D%20%2B%3D%201%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20for%20choice%20in%20choices%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E8%B6%8A%E8%BF%87%E7%AC%AC%20n%20%E9%98%B6%0A%20%20%20%20%20%20%20%20if%20state%20%2B%20choice%20%3E%20n%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20backtrack%28choices,%20state%20%2B%20choice,%20n,%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%0A%0Adef%20climbing_stairs_backtrack%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E5%9B%9E%E6%BA%AF%22%22%22%0A%20%20%20%20choices%20%3D%20%5B1,%202%5D%20%20%23%20%E5%8F%AF%E9%80%89%E6%8B%A9%E5%90%91%E4%B8%8A%E7%88%AC%201%20%E9%98%B6%E6%88%96%202%20%E9%98%B6%0A%20%20%20%20state%20%3D%200%20%20%23%20%E4%BB%8E%E7%AC%AC%200%20%E9%98%B6%E5%BC%80%E5%A7%8B%E7%88%AC%0A%20%20%20%20res%20%3D%20%5B0%5D%20%20%23%20%E4%BD%BF%E7%94%A8%20res%5B0%5D%20%E8%AE%B0%E5%BD%95%E6%96%B9%E6%A1%88%E6%95%B0%E9%87%8F%0A%20%20%20%20backtrack%28choices,%20state,%20n,%20res%29%0A%20%20%20%20return%20res%5B0%5D%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_backtrack%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20backtrack%28choices%3A%20list%5Bint%5D,%20state%3A%20int,%20n%3A%20int,%20res%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9B%9E%E6%BA%AF%22%22%22%0A%20%20%20%20%23%20%E5%BD%93%E7%88%AC%E5%88%B0%E7%AC%AC%20n%20%E9%98%B6%E6%97%B6%EF%BC%8C%E6%96%B9%E6%A1%88%E6%95%B0%E9%87%8F%E5%8A%A0%201%0A%20%20%20%20if%20state%20%3D%3D%20n%3A%0A%20%20%20%20%20%20%20%20res%5B0%5D%20%2B%3D%201%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%80%89%E6%8B%A9%0A%20%20%20%20for%20choice%20in%20choices%3A%0A%20%20%20%20%20%20%20%20%23%20%E5%89%AA%E6%9E%9D%EF%BC%9A%E4%B8%8D%E5%85%81%E8%AE%B8%E8%B6%8A%E8%BF%87%E7%AC%AC%20n%20%E9%98%B6%0A%20%20%20%20%20%20%20%20if%20state%20%2B%20choice%20%3E%20n%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%23%20%E5%B0%9D%E8%AF%95%EF%BC%9A%E5%81%9A%E5%87%BA%E9%80%89%E6%8B%A9%EF%BC%8C%E6%9B%B4%E6%96%B0%E7%8A%B6%E6%80%81%0A%20%20%20%20%20%20%20%20backtrack%28choices,%20state%20%2B%20choice,%20n,%20res%29%0A%20%20%20%20%20%20%20%20%23%20%E5%9B%9E%E9%80%80%0A%0A%0Adef%20climbing_stairs_backtrack%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E5%9B%9E%E6%BA%AF%22%22%22%0A%20%20%20%20choices%20%3D%20%5B1,%202%5D%20%20%23%20%E5%8F%AF%E9%80%89%E6%8B%A9%E5%90%91%E4%B8%8A%E7%88%AC%201%20%E9%98%B6%E6%88%96%202%20%E9%98%B6%0A%20%20%20%20state%20%3D%200%20%20%23%20%E4%BB%8E%E7%AC%AC%200%20%E9%98%B6%E5%BC%80%E5%A7%8B%E7%88%AC%0A%20%20%20%20res%20%3D%20%5B0%5D%20%20%23%20%E4%BD%BF%E7%94%A8%20res%5B0%5D%20%E8%AE%B0%E5%BD%95%E6%96%B9%E6%A1%88%E6%95%B0%E9%87%8F%0A%20%20%20%20backtrack%28choices,%20state,%20n,%20res%29%0A%20%20%20%20return%20res%5B0%5D%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%204%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_backtrack%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
2024-01-07 23:42:54 +08:00
2023-10-06 13:31:21 +08:00
## 14.1.1 &nbsp; 方法一:暴力搜索
2023-12-02 06:24:05 +08:00
回溯算法通常并不显式地对问题进行拆解,而是将求解问题看作一系列决策步骤,通过试探和剪枝,搜索所有可能的解。
2023-10-06 13:31:21 +08:00
2023-12-02 06:24:05 +08:00
我们可以尝试从问题分解的角度分析这道题。设爬到第 $i$ 阶共有 $dp[i]$ 种方案,那么 $dp[i]$ 就是原问题,其子问题包括:
2023-10-06 13:31:21 +08:00
$$
dp[i-1], dp[i-2], \dots, dp[2], dp[1]
$$
2023-12-02 06:24:05 +08:00
由于每轮只能上 $1$ 阶或 $2$ 阶,因此当我们站在第 $i$ 阶楼梯上时,上一轮只可能站在第 $i - 1$ 阶或第 $i - 2$ 阶上。换句话说,我们只能从第 $i -1$ 阶或第 $i - 2$ 阶迈向第 $i$ 阶。
2023-10-06 13:31:21 +08:00
由此便可得出一个重要推论:**爬到第 $i - 1$ 阶的方案数加上爬到第 $i - 2$ 阶的方案数就等于爬到第 $i$ 阶的方案数**。公式如下:
$$
dp[i] = dp[i-1] + dp[i-2]
$$
这意味着在爬楼梯问题中,各个子问题之间存在递推关系,**原问题的解可以由子问题的解构建得来**。图 14-2 展示了该递推关系。
2023-11-09 05:13:48 +08:00
![方案数量递推关系](intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 14-2 &nbsp; 方案数量递推关系 </p>
我们可以根据递推公式得到暴力搜索解法。以 $dp[n]$ 为起始点,**递归地将一个较大问题拆解为两个较小问题的和**,直至到达最小子问题 $dp[1]$ 和 $dp[2]$ 时返回。其中,最小子问题的解是已知的,即 $dp[1] = 1$、$dp[2] = 2$ ,表示爬到第 $1$、$2$ 阶分别有 $1$、$2$ 种方案。
2023-12-02 06:24:05 +08:00
观察以下代码,它和标准回溯代码都属于深度优先搜索,但更加简洁:
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="climbing_stairs_dfs.py"
2023-10-06 14:10:18 +08:00
def dfs(i: int) -> int:
"""搜索"""
# 已知 dp[1] 和 dp[2] ,返回之
if i == 1 or i == 2:
return i
# dp[i] = dp[i-1] + dp[i-2]
count = dfs(i - 1) + dfs(i - 2)
return count
def climbing_stairs_dfs(n: int) -> int:
"""爬楼梯:搜索"""
return dfs(n)
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="climbing_stairs_dfs.cpp"
2023-10-06 14:10:18 +08:00
/* 搜索 */
int dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
int climbingStairsDFS(int n) {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="climbing_stairs_dfs.java"
2023-10-06 14:10:18 +08:00
/* 搜索 */
int dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
int climbingStairsDFS(int n) {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="climbing_stairs_dfs.cs"
2023-10-06 14:10:18 +08:00
/* 搜索 */
2023-10-15 21:18:21 +08:00
int DFS(int i) {
2023-10-06 14:10:18 +08:00
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
2023-10-15 21:18:21 +08:00
int count = DFS(i - 1) + DFS(i - 2);
2023-10-06 14:10:18 +08:00
return count;
}
/* 爬楼梯:搜索 */
2023-10-08 01:43:28 +08:00
int ClimbingStairsDFS(int n) {
2023-10-15 21:18:21 +08:00
return DFS(n);
2023-10-06 14:10:18 +08:00
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="climbing_stairs_dfs.go"
2023-10-06 14:10:18 +08:00
/* 搜索 */
func dfs(i int) int {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 {
return i
}
// dp[i] = dp[i-1] + dp[i-2]
count := dfs(i-1) + dfs(i-2)
return count
}
/* 爬楼梯:搜索 */
func climbingStairsDFS(n int) int {
return dfs(n)
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="climbing_stairs_dfs.swift"
2023-10-06 14:10:18 +08:00
/* 搜索 */
func dfs(i: Int) -> Int {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 {
return i
}
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i: i - 1) + dfs(i: i - 2)
return count
}
/* 爬楼梯:搜索 */
func climbingStairsDFS(n: Int) -> Int {
dfs(i: n)
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="climbing_stairs_dfs.js"
2023-10-06 14:10:18 +08:00
/* 搜索 */
function dfs(i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i === 1 || i === 2) return i;
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
function climbingStairsDFS(n) {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="climbing_stairs_dfs.ts"
2023-10-06 14:10:18 +08:00
/* 搜索 */
function dfs(i: number): number {
// 已知 dp[1] 和 dp[2] ,返回之
if (i === 1 || i === 2) return i;
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
function climbingStairsDFS(n: number): number {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="climbing_stairs_dfs.dart"
2023-10-06 14:10:18 +08:00
/* 搜索 */
int dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2) return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
int climbingStairsDFS(int n) {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="climbing_stairs_dfs.rs"
2023-10-06 14:10:18 +08:00
/* 搜索 */
fn dfs(i: usize) -> i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 { return i as i32; }
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i - 1) + dfs(i - 2);
count
}
/* 爬楼梯:搜索 */
fn climbing_stairs_dfs(n: usize) -> i32 {
dfs(n)
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="climbing_stairs_dfs.c"
2023-10-06 14:10:18 +08:00
/* 搜索 */
int dfs(int i) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* 爬楼梯:搜索 */
int climbingStairsDFS(int n) {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="climbing_stairs_dfs.zig"
2023-10-06 14:10:18 +08:00
// 搜索
fn dfs(i: usize) i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 or i == 2) {
return @intCast(i);
}
// dp[i] = dp[i-1] + dp[i-2]
var count = dfs(i - 1) + dfs(i - 2);
return count;
}
// 爬楼梯:搜索
fn climbingStairsDFS(comptime n: usize) i32 {
return dfs(n);
}
2023-10-06 13:31:21 +08:00
```
2024-01-07 23:42:54 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201%29%20%2B%20dfs%28i%20-%202%29%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20return%20dfs%28n%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201%29%20%2B%20dfs%28i%20-%202%29%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20return%20dfs%28n%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
2024-01-07 23:42:54 +08:00
2023-10-06 13:31:21 +08:00
图 14-3 展示了暴力搜索形成的递归树。对于问题 $dp[n]$ ,其递归树的深度为 $n$ ,时间复杂度为 $O(2^n)$ 。指数阶属于爆炸式增长,如果我们输入一个比较大的 $n$ ,则会陷入漫长的等待之中。
2023-11-09 05:13:48 +08:00
![爬楼梯对应递归树](intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 14-3 &nbsp; 爬楼梯对应递归树 </p>
2023-12-02 06:24:05 +08:00
观察图 14-3 **指数阶的时间复杂度是“重叠子问题”导致的**。例如 $dp[9]$ 被分解为 $dp[8]$ 和 $dp[7]$ $dp[8]$ 被分解为 $dp[7]$ 和 $dp[6]$ ,两者都包含子问题 $dp[7]$ 。
2023-10-06 13:31:21 +08:00
2023-12-28 17:18:37 +08:00
以此类推,子问题中包含更小的重叠子问题,子子孙孙无穷尽也。绝大部分计算资源都浪费在这些重叠的子问题上。
2023-10-06 13:31:21 +08:00
## 14.1.2 &nbsp; 方法二:记忆化搜索
为了提升算法效率,**我们希望所有的重叠子问题都只被计算一次**。为此,我们声明一个数组 `mem` 来记录每个子问题的解,并在搜索过程中将重叠子问题剪枝。
1. 当首次计算 $dp[i]$ 时,我们将其记录至 `mem[i]` ,以便之后使用。
2. 当再次需要计算 $dp[i]$ 时,我们便可直接从 `mem[i]` 中获取结果,从而避免重复计算该子问题。
2023-12-02 06:24:05 +08:00
代码如下所示:
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="climbing_stairs_dfs_mem.py"
2023-10-06 14:10:18 +08:00
def dfs(i: int, mem: list[int]) -> int:
"""记忆化搜索"""
# 已知 dp[1] 和 dp[2] ,返回之
if i == 1 or i == 2:
return i
# 若存在记录 dp[i] ,则直接返回之
if mem[i] != -1:
return mem[i]
# dp[i] = dp[i-1] + dp[i-2]
count = dfs(i - 1, mem) + dfs(i - 2, mem)
# 记录 dp[i]
mem[i] = count
return count
def climbing_stairs_dfs_mem(n: int) -> int:
"""爬楼梯:记忆化搜索"""
# mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
mem = [-1] * (n + 1)
return dfs(n, mem)
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="climbing_stairs_dfs_mem.cpp"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
int dfs(int i, vector<int> &mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
int climbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
vector<int> mem(n + 1, -1);
return dfs(n, mem);
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="climbing_stairs_dfs_mem.java"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
int dfs(int i, int[] mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
int climbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
int[] mem = new int[n + 1];
Arrays.fill(mem, -1);
return dfs(n, mem);
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="climbing_stairs_dfs_mem.cs"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
2023-10-15 21:18:21 +08:00
int DFS(int i, int[] mem) {
2023-10-06 14:10:18 +08:00
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
2023-10-15 21:18:21 +08:00
int count = DFS(i - 1, mem) + DFS(i - 2, mem);
2023-10-06 14:10:18 +08:00
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
2023-10-08 01:43:28 +08:00
int ClimbingStairsDFSMem(int n) {
2023-10-06 14:10:18 +08:00
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
int[] mem = new int[n + 1];
Array.Fill(mem, -1);
2023-10-15 21:18:21 +08:00
return DFS(n, mem);
2023-10-06 14:10:18 +08:00
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="climbing_stairs_dfs_mem.go"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
func dfsMem(i int, mem []int) int {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 {
return i
}
// 若存在记录 dp[i] ,则直接返回之
if mem[i] != -1 {
return mem[i]
}
// dp[i] = dp[i-1] + dp[i-2]
count := dfsMem(i-1, mem) + dfsMem(i-2, mem)
// 记录 dp[i]
mem[i] = count
return count
}
/* 爬楼梯:记忆化搜索 */
func climbingStairsDFSMem(n int) int {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
mem := make([]int, n+1)
for i := range mem {
mem[i] = -1
}
return dfsMem(n, mem)
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="climbing_stairs_dfs_mem.swift"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
func dfs(i: Int, mem: inout [Int]) -> Int {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 {
return i
}
// 若存在记录 dp[i] ,则直接返回之
if mem[i] != -1 {
return mem[i]
}
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)
// 记录 dp[i]
mem[i] = count
return count
}
/* 爬楼梯:记忆化搜索 */
func climbingStairsDFSMem(n: Int) -> Int {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
var mem = Array(repeating: -1, count: n + 1)
return dfs(i: n, mem: &mem)
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="climbing_stairs_dfs_mem.js"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
function dfs(i, mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i === 1 || i === 2) return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
function climbingStairsDFSMem(n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
const mem = new Array(n + 1).fill(-1);
return dfs(n, mem);
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="climbing_stairs_dfs_mem.ts"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
function dfs(i: number, mem: number[]): number {
// 已知 dp[1] 和 dp[2] ,返回之
if (i === 1 || i === 2) return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
const count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
function climbingStairsDFSMem(n: number): number {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
const mem = new Array(n + 1).fill(-1);
return dfs(n, mem);
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="climbing_stairs_dfs_mem.dart"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
int dfs(int i, List<int> mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2) return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
int climbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
List<int> mem = List.filled(n + 1, -1);
return dfs(n, mem);
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="climbing_stairs_dfs_mem.rs"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
fn dfs(i: usize, mem: &mut [i32]) -> i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if i == 1 || i == 2 { return i as i32; }
// 若存在记录 dp[i] ,则直接返回之
if mem[i] != -1 { return mem[i]; }
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
count
}
/* 爬楼梯:记忆化搜索 */
fn climbing_stairs_dfs_mem(n: usize) -> i32 {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
let mut mem = vec![-1; n + 1];
dfs(n, &mut mem)
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="climbing_stairs_dfs_mem.c"
2023-10-06 14:10:18 +08:00
/* 记忆化搜索 */
int dfs(int i, int *mem) {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 || i == 2)
return i;
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
/* 爬楼梯:记忆化搜索 */
int climbingStairsDFSMem(int n) {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
int *mem = (int *)malloc((n + 1) * sizeof(int));
for (int i = 0; i <= n; i++) {
mem[i] = -1;
}
int result = dfs(n, mem);
free(mem);
return result;
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="climbing_stairs_dfs_mem.zig"
2023-10-06 14:10:18 +08:00
// 记忆化搜索
fn dfs(i: usize, mem: []i32) i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if (i == 1 or i == 2) {
return @intCast(i);
}
// 若存在记录 dp[i] ,则直接返回之
if (mem[i] != -1) {
return mem[i];
}
// dp[i] = dp[i-1] + dp[i-2]
var count = dfs(i - 1, mem) + dfs(i - 2, mem);
// 记录 dp[i]
mem[i] = count;
return count;
}
// 爬楼梯:记忆化搜索
fn climbingStairsDFSMem(comptime n: usize) i32 {
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
var mem = [_]i32{ -1 } ** (n + 1);
return dfs(n, &mem);
}
2023-10-06 13:31:21 +08:00
```
2024-01-07 23:42:54 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int,%20mem%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20%E8%8B%A5%E5%AD%98%E5%9C%A8%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%20%EF%BC%8C%E5%88%99%E7%9B%B4%E6%8E%A5%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20mem%5Bi%5D%20!%3D%20-1%3A%0A%20%20%20%20%20%20%20%20return%20mem%5Bi%5D%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201,%20mem%29%20%2B%20dfs%28i%20-%202,%20mem%29%0A%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%0A%20%20%20%20mem%5Bi%5D%20%3D%20count%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs_mem%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20mem%5Bi%5D%20%E8%AE%B0%E5%BD%95%E7%88%AC%E5%88%B0%E7%AC%AC%20i%20%E9%98%B6%E7%9A%84%E6%96%B9%E6%A1%88%E6%80%BB%E6%95%B0%EF%BC%8C-1%20%E4%BB%A3%E8%A1%A8%E6%97%A0%E8%AE%B0%E5%BD%95%0A%20%20%20%20mem%20%3D%20%5B-1%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20return%20dfs%28n,%20mem%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs_mem%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int,%20mem%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20%E8%8B%A5%E5%AD%98%E5%9C%A8%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%20%EF%BC%8C%E5%88%99%E7%9B%B4%E6%8E%A5%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20mem%5Bi%5D%20!%3D%20-1%3A%0A%20%20%20%20%20%20%20%20return%20mem%5Bi%5D%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201,%20mem%29%20%2B%20dfs%28i%20-%202,%20mem%29%0A%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%0A%20%20%20%20mem%5Bi%5D%20%3D%20count%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs_mem%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20mem%5Bi%5D%20%E8%AE%B0%E5%BD%95%E7%88%AC%E5%88%B0%E7%AC%AC%20i%20%E9%98%B6%E7%9A%84%E6%96%B9%E6%A1%88%E6%80%BB%E6%95%B0%EF%BC%8C-1%20%E4%BB%A3%E8%A1%A8%E6%97%A0%E8%AE%B0%E5%BD%95%0A%20%20%20%20mem%20%3D%20%5B-1%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20return%20dfs%28n,%20mem%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs_mem%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
2024-01-07 23:42:54 +08:00
2023-12-02 06:24:05 +08:00
观察图 14-4 **经过记忆化处理后,所有重叠子问题都只需计算一次,时间复杂度优化至 $O(n)$** ,这是一个巨大的飞跃。
2023-10-06 13:31:21 +08:00
2023-11-09 05:13:48 +08:00
![记忆化搜索对应递归树](intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 14-4 &nbsp; 记忆化搜索对应递归树 </p>
## 14.1.3 &nbsp; 方法三:动态规划
2023-12-02 06:24:05 +08:00
**记忆化搜索是一种“从顶至底”的方法**:我们从原问题(根节点)开始,递归地将较大子问题分解为较小子问题,直至解已知的最小子问题(叶节点)。之后,通过回溯逐层收集子问题的解,构建出原问题的解。
2023-10-06 13:31:21 +08:00
与之相反,**动态规划是一种“从底至顶”的方法**:从最小子问题的解开始,迭代地构建更大子问题的解,直至得到原问题的解。
2023-12-02 06:24:05 +08:00
由于动态规划不包含回溯过程,因此只需使用循环迭代实现,无须使用递归。在以下代码中,我们初始化一个数组 `dp` 来存储子问题的解,它起到了与记忆化搜索中数组 `mem` 相同的记录作用:
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="climbing_stairs_dp.py"
2023-10-06 14:10:18 +08:00
def climbing_stairs_dp(n: int) -> int:
"""爬楼梯:动态规划"""
if n == 1 or n == 2:
return n
# 初始化 dp 表,用于存储子问题的解
dp = [0] * (n + 1)
# 初始状态:预设最小子问题的解
dp[1], dp[2] = 1, 2
# 状态转移:从较小子问题逐步求解较大子问题
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="climbing_stairs_dp.cpp"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用于存储子问题的解
vector<int> dp(n + 1);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="climbing_stairs_dp.java"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用于存储子问题的解
int[] dp = new int[n + 1];
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="climbing_stairs_dp.cs"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
2023-10-08 01:43:28 +08:00
int ClimbingStairsDP(int n) {
2023-10-06 14:10:18 +08:00
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用于存储子问题的解
int[] dp = new int[n + 1];
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="climbing_stairs_dp.go"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
func climbingStairsDP(n int) int {
if n == 1 || n == 2 {
return n
}
// 初始化 dp 表,用于存储子问题的解
dp := make([]int, n+1)
// 初始状态:预设最小子问题的解
dp[1] = 1
dp[2] = 2
// 状态转移:从较小子问题逐步求解较大子问题
for i := 3; i <= n; i++ {
dp[i] = dp[i-1] + dp[i-2]
}
return dp[n]
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="climbing_stairs_dp.swift"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
func climbingStairsDP(n: Int) -> Int {
if n == 1 || n == 2 {
return n
}
// 初始化 dp 表,用于存储子问题的解
var dp = Array(repeating: 0, count: n + 1)
// 初始状态:预设最小子问题的解
dp[1] = 1
dp[2] = 2
// 状态转移:从较小子问题逐步求解较大子问题
for i in stride(from: 3, through: n, by: 1) {
dp[i] = dp[i - 1] + dp[i - 2]
}
return dp[n]
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="climbing_stairs_dp.js"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
function climbingStairsDP(n) {
if (n === 1 || n === 2) return n;
// 初始化 dp 表,用于存储子问题的解
const dp = new Array(n + 1).fill(-1);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="climbing_stairs_dp.ts"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
function climbingStairsDP(n: number): number {
if (n === 1 || n === 2) return n;
// 初始化 dp 表,用于存储子问题的解
const dp = new Array(n + 1).fill(-1);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="climbing_stairs_dp.dart"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
int climbingStairsDP(int n) {
if (n == 1 || n == 2) return n;
// 初始化 dp 表,用于存储子问题的解
List<int> dp = List.filled(n + 1, 0);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="climbing_stairs_dp.rs"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
fn climbing_stairs_dp(n: usize) -> i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if n == 1 || n == 2 { return n as i32; }
// 初始化 dp 表,用于存储子问题的解
let mut dp = vec![-1; n + 1];
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for i in 3..=n {
dp[i] = dp[i - 1] + dp[i - 2];
}
dp[n]
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="climbing_stairs_dp.c"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:动态规划 */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// 初始化 dp 表,用于存储子问题的解
int *dp = (int *)malloc((n + 1) * sizeof(int));
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int result = dp[n];
free(dp);
return result;
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="climbing_stairs_dp.zig"
2023-10-06 14:10:18 +08:00
// 爬楼梯:动态规划
fn climbingStairsDP(comptime n: usize) i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if (n == 1 or n == 2) {
return @intCast(n);
}
// 初始化 dp 表,用于存储子问题的解
var dp = [_]i32{-1} ** (n + 1);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (3..n + 1) |i| {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2023-10-06 13:31:21 +08:00
```
2024-01-07 23:42:54 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20dp%20%E8%A1%A8%EF%BC%8C%E7%94%A8%E4%BA%8E%E5%AD%98%E5%82%A8%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%20%3D%20%5B0%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E7%8A%B6%E6%80%81%EF%BC%9A%E9%A2%84%E8%AE%BE%E6%9C%80%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%5B1%5D,%20dp%5B2%5D%20%3D%201,%202%0A%20%20%20%20%23%20%E7%8A%B6%E6%80%81%E8%BD%AC%E7%A7%BB%EF%BC%9A%E4%BB%8E%E8%BE%83%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E9%80%90%E6%AD%A5%E6%B1%82%E8%A7%A3%E8%BE%83%E5%A4%A7%E5%AD%90%E9%97%AE%E9%A2%98%0A%20%20%20%20for%20i%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20dp%5Bi%5D%20%3D%20dp%5Bi%20-%201%5D%20%2B%20dp%5Bi%20-%202%5D%0A%20%20%20%20return%20dp%5Bn%5D%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20dp%20%E8%A1%A8%EF%BC%8C%E7%94%A8%E4%BA%8E%E5%AD%98%E5%82%A8%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%20%3D%20%5B0%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E7%8A%B6%E6%80%81%EF%BC%9A%E9%A2%84%E8%AE%BE%E6%9C%80%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%5B1%5D,%20dp%5B2%5D%20%3D%201,%202%0A%20%20%20%20%23%20%E7%8A%B6%E6%80%81%E8%BD%AC%E7%A7%BB%EF%BC%9A%E4%BB%8E%E8%BE%83%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E9%80%90%E6%AD%A5%E6%B1%82%E8%A7%A3%E8%BE%83%E5%A4%A7%E5%AD%90%E9%97%AE%E9%A2%98%0A%20%20%20%20for%20i%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20dp%5Bi%5D%20%3D%20dp%5Bi%20-%201%5D%20%2B%20dp%5Bi%20-%202%5D%0A%20%20%20%20return%20dp%5Bn%5D%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
2024-01-07 23:42:54 +08:00
2023-10-06 13:31:21 +08:00
图 14-5 模拟了以上代码的执行过程。
2023-11-09 05:13:48 +08:00
![爬楼梯的动态规划过程](intro_to_dynamic_programming.assets/climbing_stairs_dp.png){ class="animation-figure" }
2023-10-06 13:31:21 +08:00
<p align="center"> 图 14-5 &nbsp; 爬楼梯的动态规划过程 </p>
2023-12-02 06:24:05 +08:00
与回溯算法一样,动态规划也使用“状态”概念来表示问题求解的特定阶段,每个状态都对应一个子问题以及相应的局部最优解。例如,爬楼梯问题的状态定义为当前所在楼梯阶数 $i$ 。
2023-10-06 13:31:21 +08:00
根据以上内容,我们可以总结出动态规划的常用术语。
- 将数组 `dp` 称为「$dp$ 表」,$dp[i]$ 表示状态 $i$ 对应子问题的解。
2023-12-02 06:24:05 +08:00
- 将最小子问题对应的状态(第 $1$ 阶和第 $2$ 阶楼梯)称为「初始状态」。
2023-10-06 13:31:21 +08:00
- 将递推公式 $dp[i] = dp[i-1] + dp[i-2]$ 称为「状态转移方程」。
## 14.1.4 &nbsp; 空间优化
2023-12-02 06:24:05 +08:00
细心的读者可能发现了,**由于 $dp[i]$ 只与 $dp[i-1]$ 和 $dp[i-2]$ 有关,因此我们无须使用一个数组 `dp` 来存储所有子问题的解**,而只需两个变量滚动前进即可。代码如下所示:
2023-10-06 13:31:21 +08:00
=== "Python"
```python title="climbing_stairs_dp.py"
2023-10-06 14:10:18 +08:00
def climbing_stairs_dp_comp(n: int) -> int:
"""爬楼梯:空间优化后的动态规划"""
if n == 1 or n == 2:
return n
a, b = 1, 2
for _ in range(3, n + 1):
a, b = b, a + b
return b
2023-10-06 13:31:21 +08:00
```
=== "C++"
```cpp title="climbing_stairs_dp.cpp"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "Java"
```java title="climbing_stairs_dp.java"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "C#"
```csharp title="climbing_stairs_dp.cs"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
2023-10-08 01:43:28 +08:00
int ClimbingStairsDPComp(int n) {
2023-10-06 14:10:18 +08:00
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "Go"
```go title="climbing_stairs_dp.go"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
func climbingStairsDPComp(n int) int {
if n == 1 || n == 2 {
return n
}
a, b := 1, 2
// 状态转移:从较小子问题逐步求解较大子问题
for i := 3; i <= n; i++ {
a, b = b, a+b
}
return b
}
2023-10-06 13:31:21 +08:00
```
=== "Swift"
```swift title="climbing_stairs_dp.swift"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
func climbingStairsDPComp(n: Int) -> Int {
if n == 1 || n == 2 {
return n
}
var a = 1
var b = 2
for _ in stride(from: 3, through: n, by: 1) {
(a, b) = (b, a + b)
}
return b
}
2023-10-06 13:31:21 +08:00
```
=== "JS"
```javascript title="climbing_stairs_dp.js"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
function climbingStairsDPComp(n) {
if (n === 1 || n === 2) return n;
let a = 1,
b = 2;
for (let i = 3; i <= n; i++) {
const tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "TS"
```typescript title="climbing_stairs_dp.ts"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
function climbingStairsDPComp(n: number): number {
if (n === 1 || n === 2) return n;
let a = 1,
b = 2;
for (let i = 3; i <= n; i++) {
const tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "Dart"
```dart title="climbing_stairs_dp.dart"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2) return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "Rust"
```rust title="climbing_stairs_dp.rs"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
fn climbing_stairs_dp_comp(n: usize) -> i32 {
if n == 1 || n == 2 { return n as i32; }
let (mut a, mut b) = (1, 2);
for _ in 3..=n {
let tmp = b;
b = a + b;
a = tmp;
}
b
}
2023-10-06 13:31:21 +08:00
```
=== "C"
```c title="climbing_stairs_dp.c"
2023-10-06 14:10:18 +08:00
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
=== "Zig"
```zig title="climbing_stairs_dp.zig"
2023-10-06 14:10:18 +08:00
// 爬楼梯:空间优化后的动态规划
fn climbingStairsDPComp(comptime n: usize) i32 {
if (n == 1 or n == 2) {
return @intCast(n);
}
var a: i32 = 1;
var b: i32 = 2;
for (3..n + 1) |_| {
var tmp = b;
b = a + b;
a = tmp;
}
return b;
}
2023-10-06 13:31:21 +08:00
```
2024-01-07 23:42:54 +08:00
??? pythontutor "可视化运行"
2024-01-09 16:00:24 +08:00
<div style="height: 477px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp_comp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E7%A9%BA%E9%97%B4%E4%BC%98%E5%8C%96%E5%90%8E%E7%9A%84%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20a,%20b%20%3D%201,%202%0A%20%20%20%20for%20_%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20a,%20b%20%3D%20b,%20a%20%2B%20b%0A%20%20%20%20return%20b%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp_comp%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp_comp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E7%A9%BA%E9%97%B4%E4%BC%98%E5%8C%96%E5%90%8E%E7%9A%84%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20a,%20b%20%3D%201,%202%0A%20%20%20%20for%20_%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20a,%20b%20%3D%20b,%20a%20%2B%20b%0A%20%20%20%20return%20b%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp_comp%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
2024-01-07 23:42:54 +08:00
2023-12-02 06:24:05 +08:00
观察以上代码,由于省去了数组 `dp` 占用的空间,因此空间复杂度从 $O(n)$ 降至 $O(1)$ 。
2023-10-06 13:31:21 +08:00
在动态规划问题中,当前状态往往仅与前面有限个状态有关,这时我们可以只保留必要的状态,通过“降维”来节省内存空间。**这种空间优化技巧被称为“滚动变量”或“滚动数组”**。