Data Structures and Algorithms — Greedy Algorithms
Greedy algorithms make the best local choice at each step to achieve a global solution. They're used in problems like activity selection, Huffman coding, and minimum spanning trees.
// Example: Coin Change using Greedy Approach
function coinChange(coins, amount) {
coins.sort((a, b) => b - a);
let count = 0;
for (let coin of coins) {
while (amount >= coin) {
amount -= coin;
count++;
}
}
return count;
}
console.log(coinChange([25, 10, 5, 1], 49)); // Output: 7