mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-08 13:36:06 +00: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:
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: climbing_stairs_backtrack.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_backtrack {
|
||||
/* Backtracking */
|
||||
void Backtrack(List<int> choices, int state, int n, List<int> res) {
|
||||
// When climbing to the n-th stair, add 1 to the solution count
|
||||
if (state == n)
|
||||
res[0]++;
|
||||
// Traverse all choices
|
||||
foreach (int choice in choices) {
|
||||
// Pruning: not allowed to go beyond the n-th stair
|
||||
if (state + choice > n)
|
||||
continue;
|
||||
// Attempt: make choice, update state
|
||||
Backtrack(choices, state + choice, n, res);
|
||||
// Backtrack
|
||||
}
|
||||
}
|
||||
|
||||
/* Climbing stairs: Backtracking */
|
||||
int ClimbingStairsBacktrack(int n) {
|
||||
List<int> choices = [1, 2]; // Can choose to climb up 1 or 2 stairs
|
||||
int state = 0; // Start climbing from the 0-th stair
|
||||
List<int> res = [0]; // Use res[0] to record the solution count
|
||||
Backtrack(choices, state, n, res);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsBacktrack(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: climbing_stairs_constraint_dp.cs
|
||||
* Created Time: 2023-07-03
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_constraint_dp {
|
||||
/* Climbing stairs with constraint: Dynamic programming */
|
||||
int ClimbingStairsConstraintDP(int n) {
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[,] dp = new int[n + 1, 3];
|
||||
// Initial state: preset the solution to the smallest subproblem
|
||||
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];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsConstraintDP(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_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;
|
||||
}
|
||||
|
||||
/* Climbing stairs: Search */
|
||||
int ClimbingStairsDFS(int n) {
|
||||
return DFS(n);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsDFS(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs_mem.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dfs_mem {
|
||||
/* Memoization search */
|
||||
int DFS(int i, int[] mem) {
|
||||
// Known dp[1] and dp[2], return them
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
// If record dp[i] exists, return it directly
|
||||
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;
|
||||
}
|
||||
|
||||
/* Climbing stairs: Memoization search */
|
||||
int ClimbingStairsDFSMem(int n) {
|
||||
// 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];
|
||||
Array.Fill(mem, -1);
|
||||
return DFS(n, mem);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsDFSMem(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: climbing_stairs_dp.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dp {
|
||||
/* Climbing stairs: Dynamic programming */
|
||||
int ClimbingStairsDP(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[] dp = new int[n + 1];
|
||||
// Initial state: preset the solution to the smallest subproblem
|
||||
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];
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
|
||||
int res = ClimbingStairsDP(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
|
||||
res = ClimbingStairsDPComp(n);
|
||||
Console.WriteLine($"Climbing {n} stairs has {res} solutions");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* File: coin_change.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change {
|
||||
/* Coin change: Dynamic programming */
|
||||
int CoinChangeDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// Initialize dp table
|
||||
int[,] dp = new int[n + 1, amt + 1];
|
||||
// State transition: first row and first column
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0, a] = MAX;
|
||||
}
|
||||
// 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 exceeds target amount, don't select coin i
|
||||
dp[i, a] = dp[i - 1, a];
|
||||
} else {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, amt] != MAX ? dp[n, amt] : -1;
|
||||
}
|
||||
|
||||
/* Coin change: Space-optimized dynamic programming */
|
||||
int CoinChangeDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// Initialize dp table
|
||||
int[] dp = new int[amt + 1];
|
||||
Array.Fill(dp, 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 exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// The smaller value between not selecting and selecting coin i
|
||||
dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt] != MAX ? dp[amt] : -1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 4;
|
||||
|
||||
// Dynamic programming
|
||||
int res = CoinChangeDP(coins, amt);
|
||||
Console.WriteLine("Minimum number of coins needed to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = CoinChangeDPComp(coins, amt);
|
||||
Console.WriteLine("Minimum number of coins needed to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: coin_change_ii.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change_ii {
|
||||
/* Coin change II: Dynamic programming */
|
||||
int CoinChangeIIDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// Initialize dp table
|
||||
int[,] dp = new int[n + 1, amt + 1];
|
||||
// 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 exceeds target amount, don't select coin i
|
||||
dp[i, a] = dp[i - 1, a];
|
||||
} else {
|
||||
// Sum of the two options: not selecting and selecting coin i
|
||||
dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, amt];
|
||||
}
|
||||
|
||||
/* Coin change II: Space-optimized dynamic programming */
|
||||
int CoinChangeIIDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// Initialize dp table
|
||||
int[] dp = new int[amt + 1];
|
||||
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 exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// Sum of the two options: not selecting and selecting coin i
|
||||
dp[a] = dp[a] + dp[a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 5;
|
||||
|
||||
// Dynamic programming
|
||||
int res = CoinChangeIIDP(coins, amt);
|
||||
Console.WriteLine("Number of coin combinations to make target amount is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = CoinChangeIIDPComp(coins, amt);
|
||||
Console.WriteLine("Number of coin combinations to make target amount is " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: edit_distance.cs
|
||||
* Created Time: 2023-07-14
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class edit_distance {
|
||||
/* 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 length of t
|
||||
if (i == 0)
|
||||
return j;
|
||||
// If t is empty, return length of s
|
||||
if (j == 0)
|
||||
return i;
|
||||
// If two characters are equal, skip both characters
|
||||
if (s[i - 1] == t[j - 1])
|
||||
return EditDistanceDFS(s, t, i - 1, j - 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 minimum edit steps
|
||||
return Math.Min(Math.Min(insert, delete), replace) + 1;
|
||||
}
|
||||
|
||||
/* Edit distance: Memoization search */
|
||||
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 length of t
|
||||
if (i == 0)
|
||||
return j;
|
||||
// If t is empty, return length of s
|
||||
if (j == 0)
|
||||
return i;
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][j] != -1)
|
||||
return mem[i][j];
|
||||
// If two characters are equal, skip both characters
|
||||
if (s[i - 1] == t[j - 1])
|
||||
return EditDistanceDFSMem(s, t, mem, i - 1, j - 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 minimum edit steps
|
||||
mem[i][j] = Math.Min(Math.Min(insert, delete), replace) + 1;
|
||||
return mem[i][j];
|
||||
}
|
||||
|
||||
/* Edit distance: Dynamic programming */
|
||||
int EditDistanceDP(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[,] dp = new int[n + 1, m + 1];
|
||||
// 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: 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 two characters are equal, skip both characters
|
||||
dp[i, j] = dp[i - 1, j - 1];
|
||||
} else {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, m];
|
||||
}
|
||||
|
||||
/* Edit distance: Space-optimized dynamic programming */
|
||||
int EditDistanceDPComp(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[] dp = new int[m + 1];
|
||||
// State transition: first row
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[j] = j;
|
||||
}
|
||||
// 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: rest of the columns
|
||||
for (int j = 1; j <= m; j++) {
|
||||
int temp = dp[j];
|
||||
if (s[i - 1] == t[j - 1]) {
|
||||
// If two characters are equal, skip both characters
|
||||
dp[j] = leftup;
|
||||
} else {
|
||||
// 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 next round's dp[i-1, j-1]
|
||||
}
|
||||
}
|
||||
return dp[m];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
string s = "bag";
|
||||
string t = "pack";
|
||||
int n = s.Length, m = t.Length;
|
||||
|
||||
// Brute-force search
|
||||
int res = EditDistanceDFS(s, t, n, m);
|
||||
Console.WriteLine("Change " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][];
|
||||
for (int i = 0; i <= n; i++) {
|
||||
mem[i] = new int[m + 1];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
|
||||
res = EditDistanceDFSMem(s, t, mem, n, m);
|
||||
Console.WriteLine("Change " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
|
||||
// Dynamic programming
|
||||
res = EditDistanceDP(s, t);
|
||||
Console.WriteLine("Change " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = EditDistanceDPComp(s, t);
|
||||
Console.WriteLine("Change " + s + " to " + t + " requires a minimum of " + res + " edits");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* File: knapsack.cs
|
||||
* Created Time: 2023-07-07
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class knapsack {
|
||||
/* 0-1 knapsack: Brute-force search */
|
||||
int KnapsackDFS(int[] weight, int[] val, int i, int c) {
|
||||
// If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// If exceeds knapsack capacity, can only choose not to put it in
|
||||
if (weight[i - 1] > c) {
|
||||
return KnapsackDFS(weight, val, i - 1, c);
|
||||
}
|
||||
// Calculate the maximum value of not putting in and putting in item i
|
||||
int no = KnapsackDFS(weight, val, i - 1, c);
|
||||
int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];
|
||||
// Return the larger value of the two options
|
||||
return Math.Max(no, yes);
|
||||
}
|
||||
|
||||
/* 0-1 knapsack: Memoization search */
|
||||
int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
|
||||
// If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][c] != -1) {
|
||||
return mem[i][c];
|
||||
}
|
||||
// If exceeds knapsack capacity, can only choose not to put it in
|
||||
if (weight[i - 1] > c) {
|
||||
return KnapsackDFSMem(weight, val, mem, i - 1, c);
|
||||
}
|
||||
// Calculate the maximum value of not putting in and putting in item i
|
||||
int no = KnapsackDFSMem(weight, val, mem, i - 1, c);
|
||||
int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];
|
||||
// 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 */
|
||||
int KnapsackDP(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// Initialize dp table
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// State transition
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (weight[i - 1] > c) {
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* 0-1 knapsack: Space-optimized dynamic programming */
|
||||
int KnapsackDPComp(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// Initialize dp table
|
||||
int[] dp = new int[cap + 1];
|
||||
// State transition
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// Traverse in reverse order
|
||||
for (int c = cap; c > 0; c--) {
|
||||
if (weight[i - 1] > c) {
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] weight = [10, 20, 30, 40, 50];
|
||||
int[] val = [50, 120, 150, 210, 240];
|
||||
int cap = 50;
|
||||
int n = weight.Length;
|
||||
|
||||
// Brute-force search
|
||||
int res = KnapsackDFS(weight, val, n, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Memoization search
|
||||
int[][] mem = new int[n + 1][];
|
||||
for (int i = 0; i <= n; i++) {
|
||||
mem[i] = new int[cap + 1];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
res = KnapsackDFSMem(weight, val, mem, n, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = KnapsackDP(weight, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = KnapsackDPComp(weight, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: min_cost_climbing_stairs_dp.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_cost_climbing_stairs_dp {
|
||||
/* Minimum cost climbing stairs: Dynamic programming */
|
||||
int MinCostClimbingStairsDP(int[] cost) {
|
||||
int n = cost.Length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
// Initialize dp table, used to store solutions to subproblems
|
||||
int[] dp = new int[n + 1];
|
||||
// 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
|
||||
for (int i = 3; i <= n; i++) {
|
||||
dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* Minimum cost climbing stairs: Space-optimized dynamic programming */
|
||||
int MinCostClimbingStairsDPComp(int[] cost) {
|
||||
int n = cost.Length - 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 = Math.Min(a, tmp) + cost[i];
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1];
|
||||
Console.WriteLine("Input stair cost list is");
|
||||
PrintUtil.PrintList(cost);
|
||||
|
||||
int res = MinCostClimbingStairsDP(cost);
|
||||
Console.WriteLine($"Minimum cost to climb stairs is {res}");
|
||||
|
||||
res = MinCostClimbingStairsDPComp(cost);
|
||||
Console.WriteLine($"Minimum cost to climb stairs is {res}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* File: min_path_sum.cs
|
||||
* Created Time: 2023-07-10
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_path_sum {
|
||||
/* Minimum path sum: Brute-force search */
|
||||
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 row or column index is out of bounds, return +∞ cost
|
||||
if (i < 0 || j < 0) {
|
||||
return int.MaxValue;
|
||||
}
|
||||
// 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 top-left to (i, j)
|
||||
return Math.Min(left, up) + grid[i][j];
|
||||
}
|
||||
|
||||
/* Minimum path sum: Memoization search */
|
||||
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 row or column index is out of bounds, return +∞ cost
|
||||
if (i < 0 || j < 0) {
|
||||
return int.MaxValue;
|
||||
}
|
||||
// If there's a record, return it directly
|
||||
if (mem[i][j] != -1) {
|
||||
return mem[i][j];
|
||||
}
|
||||
// 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 top-left to (i, j)
|
||||
mem[i][j] = Math.Min(left, up) + grid[i][j];
|
||||
return mem[i][j];
|
||||
}
|
||||
|
||||
/* Minimum path sum: Dynamic programming */
|
||||
int MinPathSumDP(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// Initialize dp table
|
||||
int[,] dp = new int[n, 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: 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];
|
||||
}
|
||||
}
|
||||
return dp[n - 1, m - 1];
|
||||
}
|
||||
|
||||
/* Minimum path sum: Space-optimized dynamic programming */
|
||||
int MinPathSumDPComp(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// Initialize dp table
|
||||
int[] dp = new int[m];
|
||||
dp[0] = grid[0][0];
|
||||
// State transition: first row
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = dp[j - 1] + grid[0][j];
|
||||
}
|
||||
// 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: rest of the columns
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];
|
||||
}
|
||||
}
|
||||
return dp[m - 1];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[][] grid =
|
||||
[
|
||||
[1, 3, 1, 5],
|
||||
[2, 2, 4, 2],
|
||||
[5, 3, 2, 1],
|
||||
[4, 3, 5, 2]
|
||||
];
|
||||
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
|
||||
// Brute-force search
|
||||
int res = MinPathSumDFS(grid, n - 1, m - 1);
|
||||
Console.WriteLine("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Memoization search
|
||||
int[][] mem = new int[n][];
|
||||
for (int i = 0; i < n; i++) {
|
||||
mem[i] = new int[m];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
res = MinPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
Console.WriteLine("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Dynamic programming
|
||||
res = MinPathSumDP(grid);
|
||||
Console.WriteLine("Minimum path sum from top-left to bottom-right is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = MinPathSumDPComp(grid);
|
||||
Console.WriteLine("Minimum path sum from top-left to bottom-right is " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: unbounded_knapsack.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* Unbounded knapsack: Dynamic programming */
|
||||
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// Initialize dp table
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// State transition
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* Unbounded knapsack: Space-optimized dynamic programming */
|
||||
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// Initialize dp table
|
||||
int[] dp = new int[cap + 1];
|
||||
// State transition
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// If exceeds knapsack capacity, don't select item i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// The larger value between not selecting and selecting item i
|
||||
dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] wgt = [1, 2, 3];
|
||||
int[] val = [5, 11, 15];
|
||||
int cap = 4;
|
||||
|
||||
// Dynamic programming
|
||||
int res = UnboundedKnapsackDP(wgt, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = UnboundedKnapsackDPComp(wgt, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user