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:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -7,19 +7,19 @@
package chapter_dynamic_programming;
public class unbounded_knapsack {
/* Unbounded knapsack: Dynamic programming */
/* Unbounded knapsack: dynamic programming */
static 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 */
static 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]);
}
}
@@ -54,10 +54,10 @@ public class unbounded_knapsack {
// Dynamic programming
int res = unboundedKnapsackDP(wgt, val, cap);
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
System.out.println("Maximum total item value within the knapsack capacity: " + res);
// Space-optimized dynamic programming
res = unboundedKnapsackDPComp(wgt, val, cap);
System.out.println("Maximum item value not exceeding knapsack capacity is " + res);
System.out.println("Maximum total item value within the knapsack capacity: " + res);
}
}