Skip to content

Commit

Permalink
Fix a definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Aug 26, 2023
1 parent debf42b commit 9731a46
Show file tree
Hide file tree
Showing 85 changed files with 167 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int climbingStairsDP(int n) {
return dp[n];
}

/* 爬楼梯:状态压缩后的动态规划 */
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/coin_change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int coinChangeDP(vector<int> &coins, int amt) {
return dp[n][amt] != MAX ? dp[n][amt] : -1;
}

/* 零钱兑换:状态压缩后的动态规划 */
/* 零钱兑换:空间优化后的动态规划 */
int coinChangeDPComp(vector<int> &coins, int amt) {
int n = coins.size();
int MAX = amt + 1;
Expand Down Expand Up @@ -62,7 +62,7 @@ int main() {
int res = coinChangeDP(coins, amt);
cout << "凑到目标金额所需的最少硬币数量为 " << res << endl;

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
cout << "凑到目标金额所需的最少硬币数量为 " << res << endl;

Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/coin_change_ii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int coinChangeIIDP(vector<int> &coins, int amt) {
return dp[n][amt];
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
int coinChangeIIDPComp(vector<int> &coins, int amt) {
int n = coins.size();
// 初始化 dp 表
Expand Down Expand Up @@ -60,7 +60,7 @@ int main() {
int res = coinChangeIIDP(coins, amt);
cout << "凑出目标金额的硬币组合数量为 " << res << endl;

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
cout << "凑出目标金额的硬币组合数量为 " << res << endl;

Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/edit_distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int editDistanceDP(string s, string t) {
return dp[n][m];
}

/* 编辑距离:状态压缩后的动态规划 */
/* 编辑距离:空间优化后的动态规划 */
int editDistanceDPComp(string s, string t) {
int n = s.length(), m = t.length();
vector<int> dp(m + 1, 0);
Expand Down Expand Up @@ -128,7 +128,7 @@ int main() {
res = editDistanceDP(s, t);
cout << "" << s << " 更改为 " << t << " 最少需要编辑 " << res << "\n";

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
cout << "" << s << " 更改为 " << t << " 最少需要编辑 " << res << "\n";

Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/knapsack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
return dp[n][cap];
}

/* 0-1 背包:状态压缩后的动态规划 */
/* 0-1 背包:空间优化后的动态规划 */
int knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// 初始化 dp 表
Expand Down Expand Up @@ -101,7 +101,7 @@ int main() {
res = knapsackDP(wgt, val, cap);
cout << "不超过背包容量的最大物品价值为 " << res << endl;

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = knapsackDPComp(wgt, val, cap);
cout << "不超过背包容量的最大物品价值为 " << res << endl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int minCostClimbingStairsDP(vector<int> &cost) {
return dp[n];
}

/* 爬楼梯最小代价:状态压缩后的动态规划 */
/* 爬楼梯最小代价:空间优化后的动态规划 */
int minCostClimbingStairsDPComp(vector<int> &cost) {
int n = cost.size() - 1;
if (n == 1 || n == 2)
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/min_path_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int minPathSumDP(vector<vector<int>> &grid) {
return dp[n - 1][m - 1];
}

/* 最小路径和:状态压缩后的动态规划 */
/* 最小路径和:空间优化后的动态规划 */
int minPathSumDPComp(vector<vector<int>> &grid) {
int n = grid.size(), m = grid[0].size();
// 初始化 dp 表
Expand Down Expand Up @@ -108,7 +108,7 @@ int main() {
res = minPathSumDP(grid);
cout << "从左上角到右下角的最小路径和为 " << res << endl;

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = minPathSumDPComp(grid);
cout << "从左上角到右下角的最小路径和为 " << res << endl;

Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_dynamic_programming/unbounded_knapsack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
return dp[n][cap];
}

/* 完全背包:状态压缩后的动态规划 */
/* 完全背包:空间优化后的动态规划 */
int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// 初始化 dp 表
Expand Down Expand Up @@ -56,7 +56,7 @@ int main() {
int res = unboundedKnapsackDP(wgt, val, cap);
cout << "不超过背包容量的最大物品价值为 " << res << endl;

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, val, cap);
cout << "不超过背包容量的最大物品价值为 " << res << endl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public int climbingStairsDP(int n) {
return dp[n];
}

/* 爬楼梯:状态压缩后的动态规划 */
/* 爬楼梯:空间优化后的动态规划 */
public int climbingStairsDPComp(int n) {
if (n == 1 || n == 2)
return n;
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/coin_change.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public int coinChangeDP(int[] coins, int amt) {
return dp[n, amt] != MAX ? dp[n, amt] : -1;
}

/* 零钱兑换:状态压缩后的动态规划 */
/* 零钱兑换:空间优化后的动态规划 */
public int coinChangeDPComp(int[] coins, int amt) {
int n = coins.Length;
int MAX = amt + 1;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void Test() {
int res = coinChangeDP(coins, amt);
Console.WriteLine("凑到目标金额所需的最少硬币数量为 " + res);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
Console.WriteLine("凑到目标金额所需的最少硬币数量为 " + res);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/coin_change_ii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int coinChangeIIDP(int[] coins, int amt) {
return dp[n, amt];
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
public int coinChangeIIDPComp(int[] coins, int amt) {
int n = coins.Length;
// 初始化 dp 表
Expand Down Expand Up @@ -61,7 +61,7 @@ public void Test() {
int res = coinChangeIIDP(coins, amt);
Console.WriteLine("凑出目标金额的硬币组合数量为 " + res);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
Console.WriteLine("凑出目标金额的硬币组合数量为 " + res);
}
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/edit_distance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public int editDistanceDP(string s, string t) {
return dp[n, m];
}

/* 编辑距离:状态压缩后的动态规划 */
/* 编辑距离:空间优化后的动态规划 */
public int editDistanceDPComp(string s, string t) {
int n = s.Length, m = t.Length;
int[] dp = new int[m + 1];
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Test() {
res = editDistanceDP(s, t);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
Console.WriteLine("将 " + s + " 更改为 " + t + " 最少需要编辑 " + res + " 步");
}
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/knapsack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int knapsackDP(int[] weight, int[] val, int cap) {
return dp[n, cap];
}

/* 0-1 背包:状态压缩后的动态规划 */
/* 0-1 背包:空间优化后的动态规划 */
public int knapsackDPComp(int[] weight, int[] val, int cap) {
int n = weight.Length;
// 初始化 dp 表
Expand Down Expand Up @@ -111,7 +111,7 @@ public void Test() {
res = knapsackDP(weight, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = knapsackDPComp(weight, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public int minCostClimbingStairsDP(int[] cost) {
return dp[n];
}

/* 爬楼梯最小代价:状态压缩后的动态规划 */
/* 爬楼梯最小代价:空间优化后的动态规划 */
public int minCostClimbingStairsDPComp(int[] cost) {
int n = cost.Length - 1;
if (n == 1 || n == 2)
Expand Down
4 changes: 2 additions & 2 deletions codes/csharp/chapter_dynamic_programming/min_path_sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public int minPathSumDP(int[][] grid) {
return dp[n - 1, m - 1];
}

/* 最小路径和:状态压缩后的动态规划 */
/* 最小路径和:空间优化后的动态规划 */
public int minPathSumDPComp(int[][] grid) {
int n = grid.Length, m = grid[0].Length;
// 初始化 dp 表
Expand Down Expand Up @@ -120,7 +120,7 @@ public void Test() {
res = minPathSumDP(grid);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = minPathSumDPComp(grid);
Console.WriteLine("从左上角到右下角的做小路径和为 " + res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
return dp[n, cap];
}

/* 完全背包:状态压缩后的动态规划 */
/* 完全背包:空间优化后的动态规划 */
public int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
int n = wgt.Length;
// 初始化 dp 表
Expand Down Expand Up @@ -57,7 +57,7 @@ public void Test() {
int res = unboundedKnapsackDP(wgt, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, val, cap);
Console.WriteLine("不超过背包容量的最大物品价值为 " + res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int climbingStairsDP(int n) {
return dp[n];
}

/* 爬楼梯:状态压缩后的动态规划 */
/* 爬楼梯:空间优化后的动态规划 */
int climbingStairsDPComp(int n) {
if (n == 1 || n == 2) return n;
int a = 1, b = 2;
Expand Down
4 changes: 2 additions & 2 deletions codes/dart/chapter_dynamic_programming/coin_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int coinChangeDP(List<int> coins, int amt) {
return dp[n][amt] != MAX ? dp[n][amt] : -1;
}

/* 零钱兑换:状态压缩后的动态规划 */
/* 零钱兑换:空间优化后的动态规划 */
int coinChangeDPComp(List<int> coins, int amt) {
int n = coins.length;
int MAX = amt + 1;
Expand Down Expand Up @@ -62,7 +62,7 @@ void main() {
int res = coinChangeDP(coins, amt);
print("凑到目标金额所需的最少硬币数量为 $res");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeDPComp(coins, amt);
print("凑到目标金额所需的最少硬币数量为 $res");
}
4 changes: 2 additions & 2 deletions codes/dart/chapter_dynamic_programming/coin_change_ii.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int coinChangeIIDP(List<int> coins, int amt) {
return dp[n][amt];
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
int coinChangeIIDPComp(List<int> coins, int amt) {
int n = coins.length;
// 初始化 dp 表
Expand Down Expand Up @@ -58,7 +58,7 @@ void main() {
int res = coinChangeIIDP(coins, amt);
print("凑出目标金额的硬币组合数量为 $res");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = coinChangeIIDPComp(coins, amt);
print("凑出目标金额的硬币组合数量为 $res");
}
4 changes: 2 additions & 2 deletions codes/dart/chapter_dynamic_programming/edit_distance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int editDistanceDP(String s, String t) {
return dp[n][m];
}

/* 编辑距离:状态压缩后的动态规划 */
/* 编辑距离:空间优化后的动态规划 */
int editDistanceDPComp(String s, String t) {
int n = s.length, m = t.length;
List<int> dp = List.filled(m + 1, 0);
Expand Down Expand Up @@ -119,7 +119,7 @@ void main() {
res = editDistanceDP(s, t);
print("将 " + s + " 更改为 " + t + " 最少需要编辑 $res 步");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = editDistanceDPComp(s, t);
print("将 " + s + " 更改为 " + t + " 最少需要编辑 $res 步");
}
4 changes: 2 additions & 2 deletions codes/dart/chapter_dynamic_programming/knapsack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int knapsackDP(List<int> wgt, List<int> val, int cap) {
return dp[n][cap];
}

/* 0-1 背包:状态压缩后的动态规划 */
/* 0-1 背包:空间优化后的动态规划 */
int knapsackDPComp(List<int> wgt, List<int> val, int cap) {
int n = wgt.length;
// 初始化 dp 表
Expand Down Expand Up @@ -110,7 +110,7 @@ void main() {
res = knapsackDP(wgt, val, cap);
print("不超过背包容量的最大物品价值为 $res");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = knapsackDPComp(wgt, val, cap);
print("不超过背包容量的最大物品价值为 $res");
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int minCostClimbingStairsDP(List<int> cost) {
return dp[n];
}

/* 爬楼梯最小代价:状态压缩后的动态规划 */
/* 爬楼梯最小代价:空间优化后的动态规划 */
int minCostClimbingStairsDPComp(List<int> cost) {
int n = cost.length - 1;
if (n == 1 || n == 2) return cost[n];
Expand Down
4 changes: 2 additions & 2 deletions codes/dart/chapter_dynamic_programming/min_path_sum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int minPathSumDP(List<List<int>> grid) {
return dp[n - 1][m - 1];
}

/* 最小路径和:状态压缩后的动态规划 */
/* 最小路径和:空间优化后的动态规划 */
int minPathSumDPComp(List<List<int>> grid) {
int n = grid.length, m = grid[0].length;
// 初始化 dp 表
Expand Down Expand Up @@ -114,7 +114,7 @@ void main() {
res = minPathSumDP(grid);
print("从左上角到右下角的做小路径和为 $res");

// 状态压缩后的动态规划
// 空间优化后的动态规划
res = minPathSumDPComp(grid);
print("从左上角到右下角的做小路径和为 $res");
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {
return dp[n][cap];
}

/* 完全背包:状态压缩后的动态规划 */
/* 完全背包:空间优化后的动态规划 */
int unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {
int n = wgt.length;
// 初始化 dp 表
Expand Down Expand Up @@ -56,7 +56,7 @@ void main() {
int res = unboundedKnapsackDP(wgt, val, cap);
print("不超过背包容量的最大物品价值为 $res");

// 状态压缩后的动态规划
// 空间优化后的动态规划
int resComp = unboundedKnapsackDPComp(wgt, val, cap);
print("不超过背包容量的最大物品价值为 $resComp");
}
2 changes: 1 addition & 1 deletion codes/go/chapter_dynamic_programming/climbing_stairs_dp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func climbingStairsDP(n int) int {
return dp[n]
}

/* 爬楼梯:状态压缩后的动态规划 */
/* 爬楼梯:空间优化后的动态规划 */
func climbingStairsDPComp(n int) int {
if n == 1 || n == 2 {
return n
Expand Down
2 changes: 1 addition & 1 deletion codes/go/chapter_dynamic_programming/coin_change_ii.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func coinChangeIIDP(coins []int, amt int) int {
return dp[n][amt]
}

/* 零钱兑换 II:状态压缩后的动态规划 */
/* 零钱兑换 II:空间优化后的动态规划 */
func coinChangeIIDPComp(coins []int, amt int) int {
n := len(coins)
// 初始化 dp 表
Expand Down
Loading

0 comments on commit 9731a46

Please sign in to comment.