Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions

View File

@@ -9,25 +9,25 @@
/* 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
// When climbing to the n-th stair, add 1 to the solution count
if (state == n)
res[0]++;
// Traverse all choices
for (auto &choice : choices) {
// Pruning: do not allow climbing beyond the nth step
// Pruning: not allowed to go beyond the n-th stair
if (state + choice > n)
continue;
// Attempt: make a choice, update the state
// Attempt: make choice, update state
backtrack(choices, state + choice, n, res);
// Retract
// Backtrack
}
}
/* 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
vector<int> choices = {1, 2}; // Can choose to climb up 1 or 2 stairs
int state = 0; // Start climbing from the 0-th stair
vector<int> res = {0}; // Use res[0] to record the solution count
backtrack(choices, state, n, res);
return res[0];
}
@@ -37,7 +37,7 @@ int main() {
int n = 9;
int res = climbingStairsBacktrack(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
return 0;
}

View File

@@ -6,14 +6,14 @@
#include "../utils/common.hpp"
/* Constrained climbing stairs: Dynamic programming */
/* Climbing stairs with constraint: Dynamic programming */
int climbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return 1;
}
// Initialize dp table, used to store subproblem solutions
// Initialize dp table, used to store solutions to subproblems
vector<vector<int>> dp(n + 1, vector<int>(3, 0));
// Initial state: preset the smallest subproblem solution
// Initial state: preset the solution to the smallest subproblem
dp[1][1] = 1;
dp[1][2] = 0;
dp[2][1] = 0;
@@ -31,7 +31,7 @@ int main() {
int n = 9;
int res = climbingStairsConstraintDP(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
return 0;
}

View File

@@ -26,7 +26,7 @@ int main() {
int n = 9;
int res = climbingStairsDFS(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
return 0;
}

View File

@@ -6,12 +6,12 @@
#include "../utils/common.hpp"
/* Memoized search */
/* Memoization 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 record dp[i] exists, return it directly
if (mem[i] != -1)
return mem[i];
// dp[i] = dp[i-1] + dp[i-2]
@@ -21,9 +21,9 @@ int dfs(int i, vector<int> &mem) {
return count;
}
/* Climbing stairs: Memoized search */
/* Climbing stairs: Memoization search */
int climbingStairsDFSMem(int n) {
// mem[i] records the total number of solutions for climbing to the ith step, -1 means no record
// mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record
vector<int> mem(n + 1, -1);
return dfs(n, mem);
}
@@ -33,7 +33,7 @@ int main() {
int n = 9;
int res = climbingStairsDFSMem(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
return 0;
}

View File

@@ -10,9 +10,9 @@
int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize dp table, used to store subproblem solutions
// Initialize dp table, used to store solutions to subproblems
vector<int> dp(n + 1);
// Initial state: preset the smallest subproblem solution
// Initial state: preset the solution to the smallest subproblem
dp[1] = 1;
dp[2] = 2;
// State transition: gradually solve larger subproblems from smaller ones
@@ -40,10 +40,10 @@ int main() {
int n = 9;
int res = climbingStairsDP(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
res = climbingStairsDPComp(n);
cout << "There are " << res << " solutions to climb " << n << " stairs" << endl;
cout << "Climbing " << n << " stairs has " << res << " solutions" << endl;
return 0;
}

View File

@@ -16,14 +16,14 @@ int coinChangeDP(vector<int> &coins, int amt) {
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// State transition: the rest of the rows and columns
// State transition: 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
// If exceeds target amount, don't select coin i
dp[i][a] = dp[i - 1][a];
} else {
// The smaller value between not choosing and choosing coin i
// The smaller value between not selecting and selecting coin i
dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
}
}
@@ -42,10 +42,10 @@ int coinChangeDPComp(vector<int> &coins, int amt) {
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
// If exceeds target amount, don't select coin i
dp[a] = dp[a];
} else {
// The smaller value between not choosing and choosing coin i
// The smaller value between not selecting and selecting coin i
dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);
}
}
@@ -60,11 +60,11 @@ int main() {
// Dynamic programming
int res = coinChangeDP(coins, amt);
cout << "The minimum number of coins required to make up the target amount is " << res << endl;
cout << "Minimum number of coins needed to make target amount is " << res << endl;
// Space-optimized dynamic programming
res = coinChangeDPComp(coins, amt);
cout << "The minimum number of coins required to make up the target amount is " << res << endl;
cout << "Minimum number of coins needed to make target amount is " << res << endl;
return 0;
}

View File

@@ -19,10 +19,10 @@ int coinChangeIIDP(vector<int> &coins, int amt) {
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
// If exceeds target amount, don't select coin i
dp[i][a] = dp[i - 1][a];
} else {
// The sum of the two options of not choosing and choosing coin i
// Sum of the two options: not selecting and selecting coin i
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];
}
}
@@ -40,10 +40,10 @@ int coinChangeIIDPComp(vector<int> &coins, int amt) {
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
// If exceeds target amount, don't select coin i
dp[a] = dp[a];
} else {
// The sum of the two options of not choosing and choosing coin i
// Sum of the two options: not selecting and selecting coin i
dp[a] = dp[a] + dp[a - coins[i - 1]];
}
}
@@ -58,11 +58,11 @@ int main() {
// Dynamic programming
int res = coinChangeIIDP(coins, amt);
cout << "The number of coin combinations to make up the target amount is " << res << endl;
cout << "Number of coin combinations to make target amount is " << res << endl;
// Space-optimized dynamic programming
res = coinChangeIIDPComp(coins, amt);
cout << "The number of coin combinations to make up the target amount is " << res << endl;
cout << "Number of coin combinations to make target amount is " << res << endl;
return 0;
}

View File

@@ -6,50 +6,50 @@
#include "../utils/common.hpp"
/* Edit distance: Brute force search */
/* Edit distance: Brute-force search */
int editDistanceDFS(string s, string t, int i, int j) {
// If both s and t are empty, return 0
if (i == 0 && j == 0)
return 0;
// If s is empty, return the length of t
// If s is empty, return length of t
if (i == 0)
return j;
// If t is empty, return the length of s
// If t is empty, return length of s
if (j == 0)
return i;
// If the two characters are equal, skip these two characters
// If two characters are equal, skip both characters
if (s[i - 1] == t[j - 1])
return editDistanceDFS(s, t, i - 1, j - 1);
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
int insert = editDistanceDFS(s, t, i, j - 1);
int del = editDistanceDFS(s, t, i - 1, j);
int replace = editDistanceDFS(s, t, i - 1, j - 1);
// Return the minimum number of edits
// Return minimum edit steps
return min(min(insert, del), replace) + 1;
}
/* Edit distance: Memoized search */
/* Edit distance: Memoization search */
int editDistanceDFSMem(string s, string t, vector<vector<int>> &mem, int i, int j) {
// If both s and t are empty, return 0
if (i == 0 && j == 0)
return 0;
// If s is empty, return the length of t
// If s is empty, return length of t
if (i == 0)
return j;
// If t is empty, return the length of s
// If t is empty, return length of s
if (j == 0)
return i;
// If there is a record, return it
// If there's a record, return it directly
if (mem[i][j] != -1)
return mem[i][j];
// If the two characters are equal, skip these two characters
// If two characters are equal, skip both characters
if (s[i - 1] == t[j - 1])
return editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
int insert = editDistanceDFSMem(s, t, mem, i, j - 1);
int del = editDistanceDFSMem(s, t, mem, i - 1, j);
int replace = editDistanceDFSMem(s, t, mem, i - 1, j - 1);
// Record and return the minimum number of edits
// Record and return minimum edit steps
mem[i][j] = min(min(insert, del), replace) + 1;
return mem[i][j];
}
@@ -65,14 +65,14 @@ int editDistanceDP(string s, string t) {
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// State transition: the rest of the rows and columns
// State transition: 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
// If two characters are equal, skip both 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
// Minimum edit steps = minimum edit steps of insert, delete, replace + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
}
}
@@ -88,22 +88,22 @@ int editDistanceDPComp(string s, string t) {
for (int j = 1; j <= m; j++) {
dp[j] = j;
}
// State transition: the rest of the rows
// State transition: 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
// State transition: 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
// If two characters are equal, skip both characters
dp[j] = leftup;
} else {
// The minimum number of edits = the minimum number of edits from three operations (insert, remove, replace) + 1
// Minimum edit steps = minimum edit steps of insert, delete, 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]
leftup = temp; // Update for next round's dp[i-1, j-1]
}
}
return dp[m];
@@ -115,22 +115,22 @@ int main() {
string t = "pack";
int n = s.length(), m = t.length();
// Brute force search
// Brute-force search
int res = editDistanceDFS(s, t, n, m);
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits.\n";
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits";
// Memoized search
// Memoization search
vector<vector<int>> mem(n + 1, vector<int>(m + 1, -1));
res = editDistanceDFSMem(s, t, mem, n, m);
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits.\n";
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits";
// Dynamic programming
res = editDistanceDP(s, t);
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits.\n";
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits";
// Space-optimized dynamic programming
res = editDistanceDPComp(s, t);
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits.\n";
cout << "Changing " << s << " to " << t << " requires a minimum of " << res << " edits";
return 0;
}

View File

@@ -4,46 +4,46 @@
using namespace std;
/* 0-1 Knapsack: Brute force search */
/* 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 all items have been selected or 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 exceeds knapsack capacity, can only choose not to put it in
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 the larger value of the two options
return max(no, yes);
}
/* 0-1 Knapsack: Memoized search */
/* 0-1 knapsack: Memoization 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 all items have been selected or knapsack has no remaining capacity, return value 0
if (i == 0 || c == 0) {
return 0;
}
// If there is a record, return it
// If there's a record, return it directly
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 exceeds knapsack capacity, can only choose not to put it in
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
// Record and return the larger value of the two options
mem[i][c] = max(no, yes);
return mem[i][c];
}
/* 0-1 Knapsack: Dynamic programming */
/* 0-1 knapsack: Dynamic programming */
int knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
@@ -52,10 +52,10 @@ int knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
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
// If exceeds knapsack capacity, don't select item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
// The larger value between not selecting and selecting item i
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
}
}
@@ -63,7 +63,7 @@ int knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
return dp[n][cap];
}
/* 0-1 Knapsack: Space-optimized dynamic programming */
/* 0-1 knapsack: Space-optimized dynamic programming */
int knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
@@ -73,7 +73,7 @@ int knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
// 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
// The larger value between not selecting and selecting item i
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
@@ -88,22 +88,22 @@ int main() {
int cap = 50;
int n = wgt.size();
// Brute force search
// Brute-force search
int res = knapsackDFS(wgt, val, n, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
// Memoized search
// Memoization search
vector<vector<int>> mem(n + 1, vector<int>(cap + 1, -1));
res = knapsackDFSMem(wgt, val, mem, n, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
// Dynamic programming
res = knapsackDP(wgt, val, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
// Space-optimized dynamic programming
res = knapsackDPComp(wgt, val, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
return 0;
}

View File

@@ -6,14 +6,14 @@
#include "../utils/common.hpp"
/* Climbing stairs with minimum cost: Dynamic programming */
/* Minimum cost climbing stairs: 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
// Initialize dp table, used to store solutions to subproblems
vector<int> dp(n + 1);
// Initial state: preset the smallest subproblem solution
// Initial state: preset the solution to the smallest subproblem
dp[1] = cost[1];
dp[2] = cost[2];
// State transition: gradually solve larger subproblems from smaller ones
@@ -23,7 +23,7 @@ int minCostClimbingStairsDP(vector<int> &cost) {
return dp[n];
}
/* Climbing stairs with minimum cost: Space-optimized dynamic programming */
/* Minimum cost climbing stairs: Space-optimized dynamic programming */
int minCostClimbingStairsDPComp(vector<int> &cost) {
int n = cost.size() - 1;
if (n == 1 || n == 2)
@@ -40,14 +40,14 @@ int minCostClimbingStairsDPComp(vector<int> &cost) {
/* Driver Code */
int main() {
vector<int> cost = {0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1};
cout << "Input the cost list for stairs";
cout << "Input stair cost list is ";
printVector(cost);
int res = minCostClimbingStairsDP(cost);
cout << "Minimum cost to climb the stairs " << res << endl;
cout << "Minimum cost to climb stairs is " << res << endl;
res = minCostClimbingStairsDPComp(cost);
cout << "Minimum cost to climb the stairs " << res << endl;
cout << "Minimum cost to climb stairs is " << res << endl;
return 0;
}

View File

@@ -6,41 +6,41 @@
#include "../utils/common.hpp"
/* Minimum path sum: Brute force search */
/* 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 row or column index is out of bounds, return +∞ 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)
// Calculate the minimum path cost from 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 the minimum path cost from top-left to (i, j)
return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;
}
/* Minimum path sum: Memoized search */
/* Minimum path sum: Memoization 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 row or column index is out of bounds, return +∞ cost
if (i < 0 || j < 0) {
return INT_MAX;
}
// If there is a record, return it
// If there's a record, return it directly
if (mem[i][j] != -1) {
return mem[i][j];
}
// The minimum path cost from the left and top cells
// Minimum path cost for left and upper 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)
// Record and return the minimum path cost from 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];
}
@@ -59,7 +59,7 @@ int minPathSumDP(vector<vector<int>> &grid) {
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
// State transition: 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];
@@ -78,11 +78,11 @@ int minPathSumDPComp(vector<vector<int>> &grid) {
for (int j = 1; j < m; j++) {
dp[j] = dp[j - 1] + grid[0][j];
}
// State transition: the rest of the rows
// State transition: 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
// State transition: rest of the columns
for (int j = 1; j < m; j++) {
dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];
}
@@ -95,22 +95,22 @@ int main() {
vector<vector<int>> grid = {{1, 3, 1, 5}, {2, 2, 4, 2}, {5, 3, 2, 1}, {4, 3, 5, 2}};
int n = grid.size(), m = grid[0].size();
// Brute force search
// Brute-force search
int res = minPathSumDFS(grid, n - 1, m - 1);
cout << "The minimum path sum from the top left corner to the bottom right corner is " << res << endl;
cout << "Minimum path sum from top-left to bottom-right is " << res << endl;
// Memoized search
// Memoization search
vector<vector<int>> mem(n, vector<int>(m, -1));
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
cout << "The minimum path sum from the top left corner to the bottom right corner is " << res << endl;
cout << "Minimum path sum from top-left to bottom-right is " << res << endl;
// Dynamic programming
res = minPathSumDP(grid);
cout << "The minimum path sum from the top left corner to the bottom right corner is " << res << endl;
cout << "Minimum path sum from top-left to bottom-right is " << res << endl;
// Space-optimized dynamic programming
res = minPathSumDPComp(grid);
cout << "The minimum path sum from the top left corner to the bottom right corner is " << res << endl;
cout << "Minimum path sum from top-left to bottom-right is " << res << endl;
return 0;
}

View File

@@ -6,7 +6,7 @@
#include "../utils/common.hpp"
/* Complete knapsack: Dynamic programming */
/* Unbounded knapsack: Dynamic programming */
int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
@@ -15,10 +15,10 @@ int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
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
// If exceeds knapsack capacity, don't select item i
dp[i][c] = dp[i - 1][c];
} else {
// The greater value between not choosing and choosing item i
// The larger value between not selecting and selecting item i
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
}
}
@@ -26,7 +26,7 @@ int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
return dp[n][cap];
}
/* Complete knapsack: Space-optimized dynamic programming */
/* Unbounded knapsack: Space-optimized dynamic programming */
int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
@@ -35,10 +35,10 @@ int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
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
// If exceeds knapsack capacity, don't select item i
dp[c] = dp[c];
} else {
// The greater value between not choosing and choosing item i
// The larger value between not selecting and selecting item i
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
}
}
@@ -54,11 +54,11 @@ int main() {
// Dynamic programming
int res = unboundedKnapsackDP(wgt, val, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
// Space-optimized dynamic programming
res = unboundedKnapsackDPComp(wgt, val, cap);
cout << "The maximum value within the bag capacity is " << res << endl;
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
return 0;
}