GreedyAnimated
Minimum Coin Change (Greedy)
Always take the largest coin that fits. Optimal only for canonical coin systems.
Time
O(n)
Extra space
O(1)
Use it for
Standard currency.
How it works
- Always take the largest coin that still fits, as many times as you can.
- Repeat with the remainder.
- For standard systems (1, 5, 10, 25) this gives the fewest coins. For odd systems (like 1, 3, 4) it can be wrong, which is why the answer below also shows the true optimum from dynamic programming.
Watch it run
Loading animation…