mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-08 21:32:15 +08:00
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:
@@ -11,26 +11,26 @@ import java.util.*;
|
||||
public class climbing_stairs_backtrack {
|
||||
/* Backtracking */
|
||||
public static void backtrack(List<Integer> choices, int state, int n, List<Integer> 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.set(0, res.get(0) + 1);
|
||||
// Traverse all choices
|
||||
for (Integer 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 */
|
||||
public static int climbingStairsBacktrack(int n) {
|
||||
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 step or 2 steps
|
||||
int state = 0; // Start climbing from the 0th step
|
||||
List<Integer> choices = Arrays.asList(1, 2); // Can choose to climb up 1 or 2 stairs
|
||||
int state = 0; // Start climbing from the 0-th stair
|
||||
List<Integer> res = new ArrayList<>();
|
||||
res.add(0); // Use res[0] to record the number of solutions
|
||||
res.add(0); // Use res[0] to record the solution count
|
||||
backtrack(choices, state, n, res);
|
||||
return res.get(0);
|
||||
}
|
||||
@@ -39,6 +39,6 @@ public class climbing_stairs_backtrack {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsBacktrack(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_constraint_dp {
|
||||
/* Constrained climbing stairs: Dynamic programming */
|
||||
/* Climbing stairs with constraint: Dynamic programming */
|
||||
static 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
|
||||
int[][] dp = new int[n + 1][3];
|
||||
// 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,6 +31,6 @@ public class climbing_stairs_constraint_dp {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsConstraintDP(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ public class climbing_stairs_dfs {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFS(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class climbing_stairs_dfs_mem {
|
||||
/* Memoized search */
|
||||
/* Memoization search */
|
||||
public static int dfs(int i, 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]
|
||||
@@ -24,9 +24,9 @@ public class climbing_stairs_dfs_mem {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Climbing stairs: Memoized search */
|
||||
/* Climbing stairs: Memoization search */
|
||||
public static 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
|
||||
int[] mem = new int[n + 1];
|
||||
Arrays.fill(mem, -1);
|
||||
return dfs(n, mem);
|
||||
@@ -36,6 +36,6 @@ public class climbing_stairs_dfs_mem {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFSMem(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,9 @@ public class climbing_stairs_dp {
|
||||
public static 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
|
||||
int[] dp = new int[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,9 +40,9 @@ public class climbing_stairs_dp {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDP(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
|
||||
res = climbingStairsDPComp(n);
|
||||
System.out.println(String.format("There are %d solutions to climb %d stairs", res, n));
|
||||
System.out.println(String.format("Climbing %d stairs has %d solutions", n, res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ public class coin_change {
|
||||
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] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
@@ -46,10 +46,10 @@ public class coin_change {
|
||||
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] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
@@ -63,10 +63,10 @@ public class coin_change {
|
||||
|
||||
// Dynamic programming
|
||||
int res = coinChangeDP(coins, amt);
|
||||
System.out.println("The minimum number of coins required to make up the target amount is " + res);
|
||||
System.out.println("Minimum number of coins needed to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = coinChangeDPComp(coins, amt);
|
||||
System.out.println("The minimum number of coins required to make up the target amount is " + res);
|
||||
System.out.println("Minimum number of coins needed to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ public class coin_change_ii {
|
||||
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]];
|
||||
}
|
||||
}
|
||||
@@ -41,10 +41,10 @@ public class coin_change_ii {
|
||||
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,10 +58,10 @@ public class coin_change_ii {
|
||||
|
||||
// Dynamic programming
|
||||
int res = coinChangeIIDP(coins, amt);
|
||||
System.out.println("The number of coin combinations to make up the target amount is " + res);
|
||||
System.out.println("Number of coin combinations to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = coinChangeIIDPComp(coins, amt);
|
||||
System.out.println("The number of coin combinations to make up the target amount is " + res);
|
||||
System.out.println("Number of coin combinations to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,50 +9,50 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class edit_distance {
|
||||
/* Edit distance: Brute force search */
|
||||
/* Edit distance: Brute-force search */
|
||||
static 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.charAt(i - 1) == t.charAt(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 delete = 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 Math.min(Math.min(insert, delete), replace) + 1;
|
||||
}
|
||||
|
||||
/* Edit distance: Memoized search */
|
||||
/* Edit distance: Memoization search */
|
||||
static int editDistanceDFSMem(String s, String t, 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.charAt(i - 1) == t.charAt(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 delete = 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] = Math.min(Math.min(insert, delete), replace) + 1;
|
||||
return mem[i][j];
|
||||
}
|
||||
@@ -68,14 +68,14 @@ public class edit_distance {
|
||||
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.charAt(i - 1) == t.charAt(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] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;
|
||||
}
|
||||
}
|
||||
@@ -91,22 +91,22 @@ public class edit_distance {
|
||||
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.charAt(i - 1) == t.charAt(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] = Math.min(Math.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];
|
||||
@@ -117,11 +117,11 @@ public class edit_distance {
|
||||
String t = "pack";
|
||||
int n = s.length(), m = t.length();
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = editDistanceDFS(s, t, n, m);
|
||||
System.out.println("Changing " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][m + 1];
|
||||
for (int[] row : mem)
|
||||
Arrays.fill(row, -1);
|
||||
|
||||
@@ -10,46 +10,46 @@ import java.util.Arrays;
|
||||
|
||||
public class knapsack {
|
||||
|
||||
/* 0-1 Knapsack: Brute force search */
|
||||
/* 0-1 knapsack: Brute-force search */
|
||||
static int knapsackDFS(int[] wgt, 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 Math.max(no, yes);
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Memoized search */
|
||||
/* 0-1 knapsack: Memoization search */
|
||||
static int knapsackDFSMem(int[] wgt, int[] val, 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] = Math.max(no, yes);
|
||||
return mem[i][c];
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Dynamic programming */
|
||||
/* 0-1 knapsack: Dynamic programming */
|
||||
static int knapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -58,10 +58,10 @@ public class knapsack {
|
||||
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] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class knapsack {
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* 0-1 Knapsack: Space-optimized dynamic programming */
|
||||
/* 0-1 knapsack: Space-optimized dynamic programming */
|
||||
static int knapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -79,7 +79,7 @@ public class knapsack {
|
||||
// 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] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -93,24 +93,24 @@ public class knapsack {
|
||||
int cap = 50;
|
||||
int n = wgt.length;
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = knapsackDFS(wgt, val, n, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][cap + 1];
|
||||
for (int[] row : mem) {
|
||||
Arrays.fill(row, -1);
|
||||
}
|
||||
res = knapsackDFSMem(wgt, val, mem, n, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = knapsackDP(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = knapsackDPComp(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class min_cost_climbing_stairs_dp {
|
||||
/* Climbing stairs with minimum cost: Dynamic programming */
|
||||
/* Minimum cost climbing stairs: Dynamic programming */
|
||||
public static int minCostClimbingStairsDP(int[] cost) {
|
||||
int n = cost.length - 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
|
||||
int[] dp = new int[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
|
||||
@@ -26,7 +26,7 @@ public class min_cost_climbing_stairs_dp {
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* Climbing stairs with minimum cost: Space-optimized dynamic programming */
|
||||
/* Minimum cost climbing stairs: Space-optimized dynamic programming */
|
||||
public static int minCostClimbingStairsDPComp(int[] cost) {
|
||||
int n = cost.length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
@@ -42,12 +42,12 @@ public class min_cost_climbing_stairs_dp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] cost = { 0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1 };
|
||||
System.out.println(String.format("Input the cost list for stairs as %s", Arrays.toString(cost)));
|
||||
System.out.println(String.format("Input staircase cost list is %s", Arrays.toString(cost)));
|
||||
|
||||
int res = minCostClimbingStairsDP(cost);
|
||||
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
|
||||
System.out.println(String.format("Minimum cost to climb staircase is %d", res));
|
||||
|
||||
res = minCostClimbingStairsDPComp(cost);
|
||||
System.out.println(String.format("Minimum cost to climb the stairs %d", res));
|
||||
System.out.println(String.format("Minimum cost to climb staircase is %d", res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,41 +9,41 @@ package chapter_dynamic_programming;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class min_path_sum {
|
||||
/* Minimum path sum: Brute force search */
|
||||
/* Minimum path sum: Brute-force search */
|
||||
static int minPathSumDFS(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 Integer.MAX_VALUE;
|
||||
}
|
||||
// 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 Math.min(left, up) + grid[i][j];
|
||||
}
|
||||
|
||||
/* Minimum path sum: Memoized search */
|
||||
/* Minimum path sum: Memoization search */
|
||||
static int minPathSumDFSMem(int[][] grid, 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 Integer.MAX_VALUE;
|
||||
}
|
||||
// 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] = Math.min(left, up) + grid[i][j];
|
||||
return mem[i][j];
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class min_path_sum {
|
||||
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] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
|
||||
@@ -81,11 +81,11 @@ public class min_path_sum {
|
||||
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] = Math.min(dp[j - 1], dp[j]) + grid[i][j];
|
||||
}
|
||||
@@ -102,24 +102,24 @@ public class min_path_sum {
|
||||
};
|
||||
int n = grid.length, m = grid[0].length;
|
||||
|
||||
// Brute force search
|
||||
// Brute-force search
|
||||
int res = minPathSumDFS(grid, n - 1, m - 1);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Memoized search
|
||||
// Memoization search
|
||||
int[][] mem = new int[n][m];
|
||||
for (int[] row : mem) {
|
||||
Arrays.fill(row, -1);
|
||||
}
|
||||
res = minPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = minPathSumDP(grid);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = minPathSumDPComp(grid);
|
||||
System.out.println("The minimum path sum from the top left corner to the bottom right corner is " + res);
|
||||
System.out.println("Minimum path sum from top-left to bottom-right is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* Complete knapsack: Dynamic programming */
|
||||
/* Unbounded knapsack: Dynamic programming */
|
||||
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -16,10 +16,10 @@ public class unbounded_knapsack {
|
||||
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] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class unbounded_knapsack {
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* Complete knapsack: Space-optimized dynamic programming */
|
||||
/* Unbounded knapsack: Space-optimized dynamic programming */
|
||||
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// Initialize dp table
|
||||
@@ -36,10 +36,10 @@ public class unbounded_knapsack {
|
||||
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] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -54,10 +54,10 @@ public class unbounded_knapsack {
|
||||
|
||||
// Dynamic programming
|
||||
int res = unboundedKnapsackDP(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = unboundedKnapsackDPComp(wgt, val, cap);
|
||||
System.out.println("The maximum value within the bag capacity is " + res);
|
||||
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user