Minimum Coins (Greedy)
Make change for a given amount using the fewest coins possible. For canonical coin systems (like US coins: 1,5,10,25), the greedy algorithm is optimal: repeatedly select the largest coin ≤ remaining amount. However, greedy fails for non-canonical systems (e.g., coins [1,3,4] for amount 6), requiring dynamic programming instead. This demonstrates when greedy works vs when it doesn't.
Visualization
Interactive visualization for Minimum Coins (Greedy)
Interactive visualization with step-by-step execution
Implementation
1function minCoins(coins: number[], amount: number): number {
2 coins.sort((a, b) => b - a);
3 let count = 0;
4
5 for (const coin of coins) {
6 count += Math.floor(amount / coin);
7 amount %= coin;
8 }
9 return amount === 0 ? count : -1;
10}Deep Dive
Theoretical Foundation
For canonical coin systems, greedy is optimal. A coin system is canonical if the greedy algorithm always produces optimal solutions. Examples: US coins {1,5,10,25,50,100}, Euro coins {1,2,5,10,20,50,100,200}. Greedy algorithm: sort coins descending, repeatedly take largest coin ≤ remaining amount. Time: O(n) where n is number of coin types. For non-canonical systems like {1,3,4}, greedy fails: amount 6 gives [4,1,1] (3 coins) but optimal is [3,3] (2 coins). Must use DP for general case.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(1)
Applications
Industry Use
Vending machine change-making
Cash register systems
Currency exchange
Automated teller machines (ATMs)
Point-of-sale systems
Use Cases
Related Algorithms
Huffman Coding
Huffman Coding is a lossless data compression algorithm that creates optimal prefix-free variable-length codes based on character frequencies. Developed by David A. Huffman in 1952 as a student at MIT, it uses a greedy approach to build a binary tree where frequent characters get shorter codes. This algorithm is fundamental in ZIP, JPEG, MP3, and many compression formats.
Activity Selection Problem
Select the maximum number of non-overlapping activities from a set, where each activity has a start and end time. This classic greedy algorithm demonstrates the greedy choice property: always selecting the activity that finishes earliest leaves the most room for remaining activities. Used in scheduling problems, resource allocation, and interval management. Achieves optimal solution with O(n log n) time complexity.
Fractional Knapsack Problem
Given items with values and weights, and a knapsack with capacity, select items (or fractions thereof) to maximize total value. Unlike the 0/1 knapsack where items must be taken whole, the fractional knapsack allows taking fractions of items. The greedy approach of taking items in order of value-to-weight ratio yields the optimal solution in O(n log n) time. This demonstrates when greedy algorithms work vs. when dynamic programming is needed.
Job Sequencing with Deadlines
Schedule jobs with deadlines and profits to maximize total profit. Each job takes 1 unit time and has a deadline and profit. The greedy strategy is to sort jobs by profit (descending) and greedily schedule each job as late as possible before its deadline. This maximizes profit while respecting constraints. Used in task scheduling, CPU job management, and project planning.