Coin Change
Build the minimum number of reusable coins for every amount and reconstruct one optimal combination.
Core idea
For each amount, try appending every coin to a solved smaller amount. The minimum reachable candidate becomes the state value, and a predecessor coin can reconstruct one solution.
Read the visualization
The amount row grows from zero toward the target. Candidate arrows point back by one coin value, while unreachable cells remain infinite until a valid predecessor appears.
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| ∅ | 1 | 0 | 0 | 0 | 0 | 0 |
| 1 | ||||||
| 2 | ||||||
| 5 |
Set amount zero to zero coins and every positive amount to unreachable.
Complexity and tradeoffs
Time: O(nA). Space: O(A). The amount A defines the pseudo-polynomial state space; unreachable states remain infinite.
Where it fits
The same unbounded DP pattern handles denominations, minimum pieces, and repeated actions. Counting combinations instead of minimizing changes the state meaning and loop order.