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

@@ -6,19 +6,19 @@
#include "../utils/common.hpp"
/* Unbounded knapsack: Dynamic programming */
/* Unbounded knapsack: dynamic programming */
int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
int n = wgt.size();
// Initialize dp table
// Initialize the dp table
vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));
// 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] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
}
}
@@ -26,19 +26,19 @@ int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
return dp[n][cap];
}
/* Unbounded 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
// Initialize the dp table
vector<int> dp(cap + 1, 0);
// 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] = 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 << "Maximum item value not exceeding knapsack capacity is " << res << endl;
cout << "Maximum total item value within the knapsack capacity: " << res << endl;
// Space-optimized dynamic programming
res = unboundedKnapsackDPComp(wgt, val, cap);
cout << "Maximum item value not exceeding knapsack capacity is " << res << endl;
cout << "Maximum total item value within the knapsack capacity: " << res << endl;
return 0;
}