mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 05:02:24 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -7,19 +7,19 @@
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* Unbounded knapsack: Dynamic programming */
|
||||
/* Unbounded knapsack: dynamic programming */
|
||||
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// Initialize dp table
|
||||
// Initialize the dp table
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// State transition
|
||||
// State transitions
|
||||
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
|
||||
// If item i exceeds the remaining capacity, do not choose it
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// The larger value between not selecting and selecting item i
|
||||
// Take the larger value obtained by excluding and including item i
|
||||
dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -27,19 +27,19 @@ public class unbounded_knapsack {
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* Unbounded knapsack: Space-optimized dynamic programming */
|
||||
/* Unbounded knapsack: space-optimized dynamic programming */
|
||||
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// Initialize dp table
|
||||
// Initialize the dp table
|
||||
int[] dp = new int[cap + 1];
|
||||
// State transition
|
||||
// State transitions
|
||||
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
|
||||
// If item i exceeds the remaining capacity, do not choose it
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// The larger value between not selecting and selecting item i
|
||||
// Take the larger value obtained by excluding and including item i
|
||||
dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -55,10 +55,10 @@ public class unbounded_knapsack {
|
||||
|
||||
// Dynamic programming
|
||||
int res = UnboundedKnapsackDP(wgt, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
Console.WriteLine("Maximum total item value within the knapsack capacity: " + res);
|
||||
|
||||
// Space-optimized dynamic programming
|
||||
res = UnboundedKnapsackDPComp(wgt, val, cap);
|
||||
Console.WriteLine("Maximum item value not exceeding knapsack capacity is " + res);
|
||||
Console.WriteLine("Maximum total item value within the knapsack capacity: " + res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user