mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 05:33:40 +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:
@@ -4,14 +4,14 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Coin change: Greedy algorithm */
|
||||
/* Coin change: greedy algorithm */
|
||||
function coinChangeGreedy(coins, amt) {
|
||||
// Assume coins array is sorted
|
||||
// Assume coins is sorted in ascending order
|
||||
let i = coins.length - 1;
|
||||
let 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--;
|
||||
}
|
||||
@@ -19,30 +19,30 @@ function coinChangeGreedy(coins, amt) {
|
||||
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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// Greedy algorithm: Can guarantee finding the global optimal solution
|
||||
/* Driver code */
|
||||
// Greedy algorithm: guaranteed to find a globally optimal solution
|
||||
let coins = [1, 5, 10, 20, 50, 100];
|
||||
let amt = 186;
|
||||
let res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`Minimum coins needed to make ${amt} is ${res}`);
|
||||
console.log(`Greedy coin count for amount ${amt}: ${res}`);
|
||||
|
||||
// Greedy algorithm: Cannot guarantee finding the global optimal solution
|
||||
// Greedy algorithm: not guaranteed to find a globally optimal solution
|
||||
coins = [1, 20, 50];
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`Minimum coins needed to make ${amt} is ${res}`);
|
||||
console.log('Actually the minimum number needed is 3, i.e., 20 + 20 + 20');
|
||||
console.log(`Greedy coin count for amount ${amt}: ${res}`);
|
||||
console.log('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 = [1, 49, 50];
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
console.log(`\ncoins = ${coins}, amt = ${amt}`);
|
||||
console.log(`Minimum coins needed to make ${amt} is ${res}`);
|
||||
console.log('Actually the minimum number needed is 2, i.e., 49 + 49');
|
||||
console.log(`Greedy coin count for amount ${amt}: ${res}`);
|
||||
console.log('The actual minimum is 2 coins: 49 + 49');
|
||||
|
||||
@@ -12,30 +12,30 @@ class Item {
|
||||
}
|
||||
}
|
||||
|
||||
/* Fractional knapsack: Greedy algorithm */
|
||||
/* Fractional knapsack: greedy algorithm */
|
||||
function fractionalKnapsack(wgt, val, cap) {
|
||||
// Create item list with two attributes: weight, value
|
||||
// Create a list of items, each with two attributes: weight and value
|
||||
const items = wgt.map((w, i) => new Item(w, 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
|
||||
items.sort((a, b) => b.v / b.w - a.v / a.w);
|
||||
// Loop for greedy selection
|
||||
// Repeatedly make greedy choices
|
||||
let res = 0;
|
||||
for (const item of 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 += (item.v / item.w) * cap;
|
||||
// No remaining capacity, so break out of the loop
|
||||
// No capacity remains, so exit the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
const wgt = [10, 20, 30, 40, 50];
|
||||
const val = [50, 120, 150, 210, 240];
|
||||
const cap = 50;
|
||||
@@ -43,4 +43,4 @@ const n = wgt.length;
|
||||
|
||||
// Greedy algorithm
|
||||
const res = fractionalKnapsack(wgt, val, cap);
|
||||
console.log(`Maximum item value not exceeding knapsack capacity is ${res}`);
|
||||
console.log(`Maximum item value within the knapsack capacity: ${res}`);
|
||||
|
||||
@@ -4,19 +4,19 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Max capacity: Greedy algorithm */
|
||||
/* Maximum container capacity: greedy algorithm */
|
||||
function maxCapacity(ht) {
|
||||
// Initialize i, j to be at both ends of the array
|
||||
// Initialize i and j at opposite ends of the array
|
||||
let i = 0,
|
||||
j = ht.length - 1;
|
||||
// Initial max capacity is 0
|
||||
// The initial maximum capacity is 0
|
||||
let 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
|
||||
const 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 += 1;
|
||||
} else {
|
||||
@@ -26,9 +26,9 @@ function maxCapacity(ht) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
const ht = [3, 8, 5, 2, 7, 7, 3, 4];
|
||||
|
||||
// Greedy algorithm
|
||||
const res = maxCapacity(ht);
|
||||
console.log(`Maximum capacity is ${res}`);
|
||||
console.log(`Maximum capacity: ${res}`);
|
||||
|
||||
@@ -4,30 +4,30 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* Max product cutting: Greedy algorithm */
|
||||
/* Maximum product after splitting: greedy algorithm */
|
||||
function maxProductCutting(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
|
||||
let a = Math.floor(n / 3);
|
||||
let 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 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 Math.pow(3, a) * 2;
|
||||
}
|
||||
// When the remainder is 0, do nothing
|
||||
// When the remainder is 0, no adjustment is needed
|
||||
return Math.pow(3, a);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
let n = 58;
|
||||
|
||||
// Greedy algorithm
|
||||
let res = maxProductCutting(n);
|
||||
console.log(`Maximum cutting product is ${res}`);
|
||||
console.log(`Maximum product after splitting: ${res}`);
|
||||
|
||||
Reference in New Issue
Block a user