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

@@ -9,14 +9,14 @@ package chapter_greedy;
import java.util.Arrays;
public class coin_change_greedy {
/* Coin change: Greedy algorithm */
/* Coin change: greedy algorithm */
static int coinChangeGreedy(int[] coins, int amt) {
// Assume coins list is sorted
// Assume the coins list is sorted
int i = coins.length - 1;
int count = 0;
// Loop to make greedy choices until no remaining amount
// Repeatedly make greedy choices until no amount remains
while (amt > 0) {
// Find the coin that is less than and closest to the remaining amount
// Find the largest coin no greater than the remaining amount
while (i > 0 && coins[i] > amt) {
i--;
}
@@ -24,32 +24,32 @@ public class coin_change_greedy {
amt -= coins[i];
count++;
}
// If no feasible solution is found, return -1
// Return -1 if no feasible solution was found
return amt == 0 ? count : -1;
}
public static void main(String[] args) {
// Greedy algorithm: Can guarantee finding the global optimal solution
// Greedy algorithm: guaranteed to find a globally optimal solution
int[] coins = { 1, 5, 10, 20, 50, 100 };
int amt = 186;
int res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
System.out.println("For amount " + amt + " the greedy coin count is " + res);
// Greedy algorithm: Cannot guarantee finding the global optimal solution
// Greedy algorithm: not guaranteed to find a globally optimal solution
coins = new int[] { 1, 20, 50 };
amt = 60;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
System.out.println("Actually the minimum number needed is 3, i.e., 20 + 20 + 20");
System.out.println("For amount " + amt + " the greedy coin count is " + res);
System.out.println("The actual minimum is 3 coins: 20 + 20 + 20");
// Greedy algorithm: Cannot guarantee finding the global optimal solution
// Greedy algorithm: not guaranteed to find a globally optimal solution
coins = new int[] { 1, 49, 50 };
amt = 98;
res = coinChangeGreedy(coins, amt);
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
System.out.println("Minimum number of coins needed to make " + amt + " is " + res);
System.out.println("Actually the minimum number needed is 2, i.e., 49 + 49");
System.out.println("For amount " + amt + " the greedy coin count is " + res);
System.out.println("The actual minimum is 2 coins: 49 + 49");
}
}

View File

@@ -21,26 +21,26 @@ class Item {
}
public class fractional_knapsack {
/* Fractional knapsack: Greedy algorithm */
/* Fractional knapsack: greedy algorithm */
static double fractionalKnapsack(int[] wgt, int[] val, int cap) {
// Create item list with two attributes: weight, value
// Create a list of items, each with two attributes: weight and value
Item[] items = new Item[wgt.length];
for (int i = 0; i < wgt.length; i++) {
items[i] = new Item(wgt[i], val[i]);
}
// Sort by unit value item.v / item.w from high to low
// Sort by value per unit weight, item.v / item.w, in descending order
Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));
// Loop for greedy selection
// Repeatedly make greedy choices
double res = 0;
for (Item item : items) {
if (item.w <= cap) {
// If remaining capacity is sufficient, put the entire current item into the knapsack
// If enough capacity remains, put the entire current item in the knapsack
res += item.v;
cap -= item.w;
} else {
// If remaining capacity is insufficient, put part of the current item into the knapsack
// If the remaining capacity is insufficient, put part of the current item in the knapsack
res += (double) item.v / item.w * cap;
// No remaining capacity, so break out of the loop
// No capacity remains, so exit the loop
break;
}
}
@@ -54,6 +54,6 @@ public class fractional_knapsack {
// Greedy algorithm
double res = fractionalKnapsack(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);
}
}

View File

@@ -7,18 +7,18 @@
package chapter_greedy;
public class max_capacity {
/* Max capacity: Greedy algorithm */
/* Maximum container capacity: greedy algorithm */
static int maxCapacity(int[] ht) {
// Initialize i, j to be at both ends of the array
// Initialize i and j at opposite ends of the array
int i = 0, j = ht.length - 1;
// Initial max capacity is 0
// The initial maximum capacity is 0
int res = 0;
// Loop for greedy selection until the two boards meet
// Repeatedly make greedy choices until the two sides meet
while (i < j) {
// Update max capacity
// Update the maximum capacity
int cap = Math.min(ht[i], ht[j]) * (j - i);
res = Math.max(res, cap);
// Move the shorter board inward
// Move the shorter side inward
if (ht[i] < ht[j]) {
i++;
} else {
@@ -33,6 +33,6 @@ public class max_capacity {
// Greedy algorithm
int res = maxCapacity(ht);
System.out.println("Maximum capacity is " + res);
System.out.println("Maximum container capacity: " + res);
}
}

View File

@@ -9,24 +9,24 @@ package chapter_greedy;
import java.lang.Math;
public class max_product_cutting {
/* Max product cutting: Greedy algorithm */
/* Maximum product after splitting: greedy algorithm */
public static int maxProductCutting(int n) {
// When n <= 3, must cut out a 1
// When n <= 3, one part must be 1
if (n <= 3) {
return 1 * (n - 1);
}
// Greedily cut out 3, a is the number of 3s, b is the remainder
// Greedily split off parts of size 3; a is the number of 3s and b is the remainder
int a = n / 3;
int b = n % 3;
if (b == 1) {
// When the remainder is 1, convert a pair of 1 * 3 to 2 * 2
// When the remainder is 1, replace one pair 1 * 3 with 2 * 2
return (int) Math.pow(3, a - 1) * 2 * 2;
}
if (b == 2) {
// When the remainder is 2, do nothing
// When the remainder is 2, no adjustment is needed
return (int) Math.pow(3, a) * 2;
}
// When the remainder is 0, do nothing
// When the remainder is 0, no adjustment is needed
return (int) Math.pow(3, a);
}
@@ -35,6 +35,6 @@ public class max_product_cutting {
// Greedy algorithm
int res = maxProductCutting(n);
System.out.println("Maximum cutting product is " + res);
System.out.println("Maximum product after splitting: " + res);
}
}