mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 05:56:06 +00:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
add_executable(coin_change_greedy coin_change_greedy.cpp)
|
||||
add_executable(fractional_knapsack fractional_knapsack.cpp)
|
||||
add_executable(max_capacity max_capacity.cpp)
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: coin_change_greedy.cpp
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Coin change: Greedy */
|
||||
int coinChangeGreedy(vector<int> &coins, int amt) {
|
||||
// Assume coins list is ordered
|
||||
int i = coins.size() - 1;
|
||||
int count = 0;
|
||||
// Loop for greedy selection until no remaining amount
|
||||
while (amt > 0) {
|
||||
// Find the smallest coin close to and less than the remaining amount
|
||||
while (i > 0 && coins[i] > amt) {
|
||||
i--;
|
||||
}
|
||||
// Choose coins[i]
|
||||
amt -= coins[i];
|
||||
count++;
|
||||
}
|
||||
// If no feasible solution is found, return -1
|
||||
return amt == 0 ? count : -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// Greedy: can ensure finding a global 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 << "The minimum number of coins required to make up " << amt << " is " << res << endl;
|
||||
|
||||
// Greedy: cannot ensure finding a global optimal solution
|
||||
coins = {1, 20, 50};
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << "The minimum number of coins required to make up " << amt << " is " << res << endl;
|
||||
cout << "In reality, the minimum number needed is 3, i.e., 20 + 20 + 20" << endl;
|
||||
|
||||
// Greedy: cannot ensure finding a global optimal solution
|
||||
coins = {1, 49, 50};
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << "The minimum number of coins required to make up " << amt << " is " << res << endl;
|
||||
cout << "In reality, the minimum number needed is 2, i.e., 49 + 49" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: fractional_knapsack.cpp
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Item */
|
||||
class Item {
|
||||
public:
|
||||
int w; // Item weight
|
||||
int v; // Item value
|
||||
|
||||
Item(int w, int v) : w(w), v(v) {
|
||||
}
|
||||
};
|
||||
|
||||
/* Fractional knapsack: Greedy */
|
||||
double fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {
|
||||
// Create an item list, containing two properties: weight, 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(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });
|
||||
// Loop for greedy selection
|
||||
double res = 0;
|
||||
for (auto &item : items) {
|
||||
if (item.w <= cap) {
|
||||
// If the remaining capacity is sufficient, put the entire item into the knapsack
|
||||
res += item.v;
|
||||
cap -= item.w;
|
||||
} else {
|
||||
// If the remaining capacity is insufficient, put part of the item into the knapsack
|
||||
res += (double)item.v / item.w * cap;
|
||||
// No remaining capacity left, thus break the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> wgt = {10, 20, 30, 40, 50};
|
||||
vector<int> val = {50, 120, 150, 210, 240};
|
||||
int cap = 50;
|
||||
|
||||
// Greedy algorithm
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
cout << "The maximum value within the bag capacity is " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: max_capacity.cpp
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Maximum capacity: Greedy */
|
||||
int maxCapacity(vector<int> &ht) {
|
||||
// Initialize i, j, making them split the array at both ends
|
||||
int i = 0, j = ht.size() - 1;
|
||||
// Initial maximum capacity is 0
|
||||
int res = 0;
|
||||
// Loop for greedy selection until the two boards meet
|
||||
while (i < j) {
|
||||
// Update maximum capacity
|
||||
int cap = min(ht[i], ht[j]) * (j - i);
|
||||
res = max(res, cap);
|
||||
// Move the shorter board inward
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> ht = {3, 8, 5, 2, 7, 7, 3, 4};
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxCapacity(ht);
|
||||
cout << "The maximum capacity is " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: max_product_cutting.cpp
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Maximum product of cutting: Greedy */
|
||||
int maxProductCutting(int n) {
|
||||
// When n <= 3, must cut out a 1
|
||||
if (n <= 3) {
|
||||
return 1 * (n - 1);
|
||||
}
|
||||
// Greedy cut out 3s, a is the number of 3s, 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 into 2 * 2
|
||||
return (int)pow(3, a - 1) * 2 * 2;
|
||||
}
|
||||
if (b == 2) {
|
||||
// When the remainder is 2, do nothing
|
||||
return (int)pow(3, a) * 2;
|
||||
}
|
||||
// When the remainder is 0, do nothing
|
||||
return (int)pow(3, a);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 58;
|
||||
|
||||
// Greedy algorithm
|
||||
int res = maxProductCutting(n);
|
||||
cout << "Maximum cutting product is" << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user