mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-29 07:44:46 +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:
@@ -6,14 +6,14 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Coin change: Greedy algorithm */
|
||||
/* Coin change: greedy algorithm */
|
||||
int coinChangeGreedy(vector<int> &coins, int amt) {
|
||||
// Assume coins list is sorted
|
||||
// Assume the coins list is sorted
|
||||
int i = coins.size() - 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 that does not exceed the remaining amount
|
||||
while (i > 0 && coins[i] > amt) {
|
||||
i--;
|
||||
}
|
||||
@@ -21,40 +21,40 @@ int coinChangeGreedy(vector<int> &coins, int 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 */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
// Greedy algorithm: Can guarantee finding the global optimal solution
|
||||
// Greedy algorithm: guaranteed to find a globally optimal solution
|
||||
vector<int> coins = {1, 5, 10, 20, 50, 100};
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << "Minimum number of coins needed to make " << amt << " is " << res << endl;
|
||||
cout << "For amount " << amt << " , the greedy coin count is " << res << endl;
|
||||
|
||||
// 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);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << "Minimum number of coins needed to make " << amt << " is " << res << endl;
|
||||
cout << "Actually the minimum number needed is 3, i.e., 20 + 20 + 20" << endl;
|
||||
cout << "For amount " << amt << " , the greedy coin count is " << res << endl;
|
||||
cout << "The actual minimum is 3 coins: 20 + 20 + 20" << endl;
|
||||
|
||||
// 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);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << "Minimum number of coins needed to make " << amt << " is " << res << endl;
|
||||
cout << "Actually the minimum number needed is 2, i.e., 49 + 49" << endl;
|
||||
cout << "For amount " << amt << " , the greedy coin count is " << res << endl;
|
||||
cout << "The actual minimum is 2 coins: 49 + 49" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -16,33 +16,33 @@ class Item {
|
||||
}
|
||||
};
|
||||
|
||||
/* Fractional knapsack: Greedy algorithm */
|
||||
/* Fractional knapsack: greedy algorithm */
|
||||
double fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {
|
||||
// Create item list with two attributes: weight, value
|
||||
// Create a list of items, each with two attributes: weight and value
|
||||
vector<Item> items;
|
||||
for (int i = 0; i < wgt.size(); i++) {
|
||||
items.push_back(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
|
||||
sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });
|
||||
// Loop for greedy selection
|
||||
// Repeatedly make greedy choices
|
||||
double res = 0;
|
||||
for (auto &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;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
vector<int> wgt = {10, 20, 30, 40, 50};
|
||||
vector<int> val = {50, 120, 150, 210, 240};
|
||||
@@ -50,7 +50,7 @@ int main() {
|
||||
|
||||
// Greedy algorithm
|
||||
double res = fractionalKnapsack(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;
|
||||
}
|
||||
|
||||
@@ -6,18 +6,18 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Max capacity: Greedy algorithm */
|
||||
/* Maximum container capacity: greedy algorithm */
|
||||
int maxCapacity(vector<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.size() - 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 = min(ht[i], ht[j]) * (j - i);
|
||||
res = max(res, cap);
|
||||
// Move the shorter board inward
|
||||
// Move the shorter side inward
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
@@ -27,13 +27,13 @@ int maxCapacity(vector<int> &ht) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
vector<int> ht = {3, 8, 5, 2, 7, 7, 3, 4};
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxCapacity(ht);
|
||||
cout << "Maximum capacity is " << res << endl;
|
||||
cout << "Maximum capacity: " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,34 +6,34 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Max product cutting: Greedy algorithm */
|
||||
/* Maximum product after splitting: greedy algorithm */
|
||||
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)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)pow(3, a) * 2;
|
||||
}
|
||||
// When the remainder is 0, do nothing
|
||||
// When the remainder is 0, no adjustment is needed
|
||||
return (int)pow(3, a);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
int n = 58;
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxProductCutting(n);
|
||||
cout << "Maximum cutting product is" << res << endl;
|
||||
cout << "Maximum product after cutting: " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user