This commit is contained in:
krahets
2024-05-06 14:40:36 +08:00
parent 7e7eb6047a
commit 5c7d2c7f17
54 changed files with 3456 additions and 215 deletions
@@ -61,7 +61,22 @@ According to the state transition equation, and the initial states $dp[1] = cost
=== "C++"
```cpp title="min_cost_climbing_stairs_dp.cpp"
[class]{}-[func]{minCostClimbingStairsDP}
/* Climbing stairs with minimum cost: Dynamic programming */
int minCostClimbingStairsDP(vector<int> &cost) {
int n = cost.size() - 1;
if (n == 1 || n == 2)
return cost[n];
// Initialize dp table, used to store subproblem solutions
vector<int> dp(n + 1);
// Initial state: preset the smallest subproblem solution
dp[1] = cost[1];
dp[2] = cost[2];
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];
}
return dp[n];
}
```
=== "Java"
@@ -176,7 +191,19 @@ This problem can also be space-optimized, compressing one dimension to zero, red
=== "C++"
```cpp title="min_cost_climbing_stairs_dp.cpp"
[class]{}-[func]{minCostClimbingStairsDPComp}
/* Climbing stairs with minimum cost: Space-optimized dynamic programming */
int minCostClimbingStairsDPComp(vector<int> &cost) {
int n = cost.size() - 1;
if (n == 1 || n == 2)
return cost[n];
int a = cost[1], b = cost[2];
for (int i = 3; i <= n; i++) {
int tmp = b;
b = min(a, tmp) + cost[i];
a = tmp;
}
return b;
}
```
=== "Java"
@@ -327,7 +354,25 @@ In the end, returning $dp[n, 1] + dp[n, 2]$ will do, the sum of the two represen
=== "C++"
```cpp title="climbing_stairs_constraint_dp.cpp"
[class]{}-[func]{climbingStairsConstraintDP}
/* Constrained climbing stairs: Dynamic programming */
int climbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return 1;
}
// Initialize dp table, used to store subproblem solutions
vector<vector<int>> dp(n + 1, vector<int>(3, 0));
// Initial state: preset the smallest subproblem solution
dp[1][1] = 1;
dp[1][2] = 0;
dp[2][1] = 0;
dp[2][2] = 1;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i][1] = dp[i - 1][2];
dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
}
return dp[n][1] + dp[n][2];
}
```
=== "Java"
@@ -133,7 +133,22 @@ Implementation code as follows:
=== "C++"
```cpp title="min_path_sum.cpp"
[class]{}-[func]{minPathSumDFS}
/* Minimum path sum: Brute force search */
int minPathSumDFS(vector<vector<int>> &grid, int i, int j) {
// If it's the top-left cell, terminate the search
if (i == 0 && j == 0) {
return grid[0][0];
}
// If the row or column index is out of bounds, return a +∞ cost
if (i < 0 || j < 0) {
return INT_MAX;
}
// Calculate the minimum path cost from the top-left to (i-1, j) and (i, j-1)
int up = minPathSumDFS(grid, i - 1, j);
int left = minPathSumDFS(grid, i, j - 1);
// Return the minimum path cost from the top-left to (i, j)
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
}
```
=== "Java"
@@ -264,7 +279,27 @@ We introduce a memo list `mem` of the same size as the grid `grid`, used to reco
=== "C++"
```cpp title="min_path_sum.cpp"
[class]{}-[func]{minPathSumDFSMem}
/* Minimum path sum: Memoized search */
int minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {
// If it's the top-left cell, terminate the search
if (i == 0 && j == 0) {
return grid[0][0];
}
// If the row or column index is out of bounds, return a +∞ cost
if (i < 0 || j < 0) {
return INT_MAX;
}
// If there is a record, return it
if (mem[i][j] != -1) {
return mem[i][j];
}
// The minimum path cost from the left and top cells
int up = minPathSumDFSMem(grid, mem, i - 1, j);
int left = minPathSumDFSMem(grid, mem, i, j - 1);
// Record and return the minimum path cost from the top-left to (i, j)
mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
return mem[i][j];
}
```
=== "Java"
@@ -394,7 +429,28 @@ Implement the dynamic programming solution iteratively, code as shown below:
=== "C++"
```cpp title="min_path_sum.cpp"
[class]{}-[func]{minPathSumDP}
/* Minimum path sum: Dynamic programming */
int minPathSumDP(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
// Initialize dp table
vector<vector<int>> dp(n, vector<int>(m));
dp[0][0] = grid[0][0];
// State transition: first row
for (int j = 1; j < m; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
// State transition: first column
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// State transition: the rest of the rows and columns
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
}
}
return dp[n - 1][m - 1];
}
```
=== "Java"
@@ -563,7 +619,27 @@ Please note, since the array `dp` can only represent the state of one row, we ca
=== "C++"
```cpp title="min_path_sum.cpp"
[class]{}-[func]{minPathSumDPComp}
/* Minimum path sum: Space-optimized dynamic programming */
int minPathSumDPComp(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
// Initialize dp table
vector<int> dp(m);
// State transition: first row
dp[0] = grid[0][0];
for (int j = 1; j < m; j++) {
dp[j] = dp[j - 1] + grid[0][j];
}
// State transition: the rest of the rows
for (int i = 1; i < n; i++) {
// State transition: first column
dp[0] = dp[0] + grid[i][0];
// State transition: the rest of the columns
for (int j = 1; j < m; j++) {
dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];
}
}
return dp[m - 1];
}
```
=== "Java"
@@ -104,7 +104,31 @@ Observing the state transition equation, solving $dp[i, j]$ depends on the solut
=== "C++"
```cpp title="edit_distance.cpp"
[class]{}-[func]{editDistanceDP}
/* Edit distance: Dynamic programming */
int editDistanceDP(string s, string t) {
int n = s.length(), m = t.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
// State transition: first row and first column
for (int i = 1; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// State transition: the rest of the rows and columns
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {
// If the two characters are equal, skip these two characters
dp[i][j] = dp[i - 1][j - 1];
} else {
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
}
return dp[n][m];
}
```
=== "Java"
@@ -289,7 +313,34 @@ For this reason, we can use a variable `leftup` to temporarily store the solutio
=== "C++"
```cpp title="edit_distance.cpp"
[class]{}-[func]{editDistanceDPComp}
/* Edit distance: Space-optimized dynamic programming */
int editDistanceDPComp(string s, string t) {
int n = s.length(), m = t.length();
vector<int> dp(m + 1, 0);
// State transition: first row
for (int j = 1; j <= m; j++) {
dp[j] = j;
}
// State transition: the rest of the rows
for (int i = 1; i <= n; i++) {
// State transition: first column
int leftup = dp[0]; // Temporarily store dp[i-1, j-1]
dp[0] = i;
// State transition: the rest of the columns
for (int j = 1; j <= m; j++) {
int temp = dp[j];
if (s[i - 1] == t[j - 1]) {
// If the two characters are equal, skip these two characters
dp[j] = leftup;
} else {
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;
}
leftup = temp; // Update for the next round of dp[i-1, j-1]
}
}
return dp[m];
}
```
=== "Java"
@@ -49,9 +49,30 @@ The goal of this problem is to determine the number of ways, **considering using
=== "C++"
```cpp title="climbing_stairs_backtrack.cpp"
[class]{}-[func]{backtrack}
/* Backtracking */
void backtrack(vector<int> &choices, int state, int n, vector<int> &res) {
// When climbing to the nth step, add 1 to the number of solutions
if (state == n)
res[0]++;
// Traverse all choices
for (auto &choice : choices) {
// Pruning: do not allow climbing beyond the nth step
if (state + choice > n)
continue;
// Attempt: make a choice, update the state
backtrack(choices, state + choice, n, res);
// Retract
}
}
[class]{}-[func]{climbingStairsBacktrack}
/* Climbing stairs: Backtracking */
int climbingStairsBacktrack(int n) {
vector<int> choices = {1, 2}; // Can choose to climb up 1 step or 2 steps
int state = 0; // Start climbing from the 0th step
vector<int> res = {0}; // Use res[0] to record the number of solutions
backtrack(choices, state, n, res);
return res[0];
}
```
=== "Java"
@@ -220,9 +241,20 @@ Observe the following code, which, like standard backtracking code, belongs to d
=== "C++"
```cpp title="climbing_stairs_dfs.cpp"
[class]{}-[func]{dfs}
/* Search */
int dfs(int i) {
// Known dp[1] and dp[2], return them
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;
}
[class]{}-[func]{climbingStairsDFS}
/* Climbing stairs: Search */
int climbingStairsDFS(int n) {
return dfs(n);
}
```
=== "Java"
@@ -378,9 +410,27 @@ The code is as follows:
=== "C++"
```cpp title="climbing_stairs_dfs_mem.cpp"
[class]{}-[func]{dfs}
/* Memoized search */
int dfs(int i, vector<int> &mem) {
// Known dp[1] and dp[2], return them
if (i == 1 || i == 2)
return i;
// If there is a record for dp[i], return it
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);
// Record dp[i]
mem[i] = count;
return count;
}
[class]{}-[func]{climbingStairsDFSMem}
/* Climbing stairs: Memoized search */
int climbingStairsDFSMem(int n) {
// mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
vector<int> mem(n + 1, -1);
return dfs(n, mem);
}
```
=== "Java"
@@ -532,7 +582,21 @@ Since dynamic programming does not include a backtracking process, it only requi
=== "C++"
```cpp title="climbing_stairs_dp.cpp"
[class]{}-[func]{climbingStairsDP}
/* Climbing stairs: Dynamic programming */
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize dp table, used to store subproblem solutions
vector<int> dp(n + 1);
// Initial state: preset the smallest subproblem solution
dp[1] = 1;
dp[2] = 2;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
```
=== "Java"
@@ -655,7 +719,18 @@ Observant readers may have noticed that **since $dp[i]$ is only related to $dp[i
=== "C++"
```cpp title="climbing_stairs_dp.cpp"
[class]{}-[func]{climbingStairsDPComp}
/* Climbing stairs: Space-optimized dynamic programming */
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;
}
```
=== "Java"
@@ -83,7 +83,22 @@ The search code includes the following elements.
=== "C++"
```cpp title="knapsack.cpp"
[class]{}-[func]{knapsackDFS}
/* 0-1 Knapsack: Brute force search */
int knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
if (i == 0 || c == 0) {
return 0;
}
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
// Calculate the maximum value of not putting in and putting in item i
int no = knapsackDFS(wgt, val, i - 1, c);
int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];
// Return the greater value of the two options
return max(no, yes);
}
```
=== "Java"
@@ -214,7 +229,27 @@ After introducing memoization, **the time complexity depends on the number of su
=== "C++"
```cpp title="knapsack.cpp"
[class]{}-[func]{knapsackDFSMem}
/* 0-1 Knapsack: Memoized search */
int knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {
// If all items have been chosen or the knapsack has no remaining capacity, return value 0
if (i == 0 || c == 0) {
return 0;
}
// If there is a record, return it
if (mem[i][c] != -1) {
return mem[i][c];
}
// If exceeding the knapsack capacity, can only choose not to put it in the knapsack
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, mem, i - 1, c);
}
// Calculate the maximum value of not putting in and putting in item i
int no = knapsackDFSMem(wgt, val, mem, i - 1, c);
int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];
// Record and return the greater value of the two options
mem[i][c] = max(no, yes);
return mem[i][c];
}
```
=== "Java"
@@ -342,7 +377,25 @@ Dynamic programming essentially involves filling the $dp$ table during the state
=== "C++"
```cpp title="knapsack.cpp"
[class]{}-[func]{knapsackDP}
/* 0-1 Knapsack: Dynamic programming */
int knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
```
=== "Java"
@@ -435,7 +488,7 @@ Dynamic programming essentially involves filling the $dp$ table during the state
[class]{}-[func]{knapsackDP}
```
As shown in the figures below, both the time complexity and space complexity are determined by the size of the array `dp`, i.e., $O(n \times cap)$.
As shown in Figure 14-20, both the time complexity and space complexity are determined by the size of the array `dp`, i.e., $O(n \times cap)$.
=== "<1>"
![The dynamic programming process of the 0-1 knapsack problem](knapsack_problem.assets/knapsack_dp_step1.png){ class="animation-figure" }
@@ -538,7 +591,23 @@ In the code implementation, we only need to delete the first dimension $i$ of th
=== "C++"
```cpp title="knapsack.cpp"
[class]{}-[func]{knapsackDPComp}
/* 0-1 Knapsack: Space-optimized dynamic programming */
int knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
vector<int> dp(cap + 1, 0);
// State transition
for (int i = 1; i <= n; i++) {
// Traverse in reverse order
for (int c = cap; c >= 1; c--) {
if (wgt[i - 1] <= c) {
// The greater value between not choosing and choosing item i
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
```
=== "Java"
@@ -61,7 +61,25 @@ Comparing the code for the two problems, the state transition changes from $i-1$
=== "C++"
```cpp title="unbounded_knapsack.cpp"
[class]{}-[func]{unboundedKnapsackDP}
/* Complete knapsack: Dynamic programming */
int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[n][cap];
}
```
=== "Java"
@@ -206,7 +224,25 @@ The code implementation is quite simple, just remove the first dimension of the
=== "C++"
```cpp title="unbounded_knapsack.cpp"
[class]{}-[func]{unboundedKnapsackDPComp}
/* Complete knapsack: Space-optimized dynamic programming */
int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
vector<int> dp(cap + 1, 0);
// State transition
for (int i = 1; i <= n; i++) {
for (int c = 1; c <= cap; c++) {
if (wgt[i - 1] > c) {
// If exceeding the knapsack capacity, do not choose item i
dp[c] = dp[c];
} else {
// The greater value between not choosing and choosing item i
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
}
return dp[cap];
}
```
=== "Java"
@@ -375,7 +411,30 @@ For this reason, we use the number $amt + 1$ to represent an invalid solution, b
=== "C++"
```cpp title="coin_change.cpp"
[class]{}-[func]{coinChangeDP}
/* Coin change: Dynamic programming */
int coinChangeDP(vector<int> &coins, int amt) {
int n = coins.size();
int MAX = amt + 1;
// Initialize dp table
vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));
// State transition: first row and first column
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// State transition: the rest of the rows and columns
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[i][a] = dp[i - 1][a];
} else {
// The smaller value between not choosing and choosing coin i
dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
}
}
}
return dp[n][amt] != MAX ? dp[n][amt] : -1;
}
```
=== "Java"
@@ -552,7 +611,27 @@ The space optimization for the coin change problem is handled in the same way as
=== "C++"
```cpp title="coin_change.cpp"
[class]{}-[func]{coinChangeDPComp}
/* Coin change: Space-optimized dynamic programming */
int coinChangeDPComp(vector<int> &coins, int amt) {
int n = coins.size();
int MAX = amt + 1;
// Initialize dp table
vector<int> dp(amt + 1, MAX);
dp[0] = 0;
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[a] = dp[a];
} else {
// The smaller value between not choosing and choosing coin i
dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);
}
}
}
return dp[amt] != MAX ? dp[amt] : -1;
}
```
=== "Java"
@@ -698,7 +777,29 @@ When the target amount is $0$, no coins are needed to make up the target amount,
=== "C++"
```cpp title="coin_change_ii.cpp"
[class]{}-[func]{coinChangeIIDP}
/* Coin change II: Dynamic programming */
int coinChangeIIDP(vector<int> &coins, int amt) {
int n = coins.size();
// Initialize dp table
vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));
// Initialize first column
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[i][a] = dp[i - 1][a];
} else {
// The sum of the two options of not choosing and choosing coin i
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
}
}
}
return dp[n][amt];
}
```
=== "Java"
@@ -824,7 +925,26 @@ The space optimization approach is the same, just remove the coin dimension:
=== "C++"
```cpp title="coin_change_ii.cpp"
[class]{}-[func]{coinChangeIIDPComp}
/* Coin change II: Space-optimized dynamic programming */
int coinChangeIIDPComp(vector<int> &coins, int amt) {
int n = coins.size();
// Initialize dp table
vector<int> dp(amt + 1, 0);
dp[0] = 1;
// State transition
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
// If exceeding the target amount, do not choose coin i
dp[a] = dp[a];
} else {
// The sum of the two options of not choosing and choosing coin i
dp[a] = dp[a] + dp[a - coins[i - 1]];
}
}
}
return dp[amt];
}
```
=== "Java"