Programming Concepts — Greedy Algorithm
A greedy algorithm builds up a solution piece by piece, always choosing the option that offers the most immediate benefit.
// Example: Coin change problem
let coins = [1, 5, 10, 25];
let amount = 37;
let count = 0;
for (let i = coins.length - 1; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
console.log(count); // Output: 4