medium1-D Dynamic ProgrammingAmazonFacebook
Minimal Coin Changer
A cashier returns a specific amount using the fewest coins from an unlimited supply of given denominations.
Given coins of distinct denominations and a target amount, return the fewest coins needed to make up that amount or -1 if impossible.
Choose approach
0
0?
1?
2?
3?
4?
5?
6?
7?
8?
9?
10?
11i
1/13
Narrationstep 1 of 13
Base dp[0] = 0.
Variables
amount=11
coins=1,2,5
How to think about it
- Order of iteration (a outer, coins inner) correctly allows coin reuse.
- No valid combination -> dp[amount] stays infinity.
Edge cases to catch in interview
- amount=0 -> 0.
- No coin ≤ amount -> -1.
Examples
Example 1
-1{"coins":[2],"amount":3}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed