Coin Change
Find the fewest coins that add up to an amount, or -1, by solving every smaller amount first.
The problem
You get coin values in coins, with an unlimited supply of each, and a target amount. Return the fewest coins whose values add up to exactly amount, or -1 if no combination works. An amount of 0 needs 0 coins.
Input coins = [1, 4, 5], amount = 8
Output 2
4 + 4 uses two coins. Grabbing the biggest coin first gives 5 + 1 + 1 + 1, which uses four.
Input coins = [3, 7], amount = 5
Output -1
No mix of 3s and 7s adds up to 5.
Key constraint: The amount is at most 10,000, so a table with one entry per amount is small, while trying every combination of coins is not.
How to spot the pattern
- You want the fewest pieces that add up to a target, and pieces can be reused.
- Taking the biggest piece first can fail, so you have to compare choices.
- The best answer for an amount is built from the best answers for smaller amounts.
The idea
State: fewest[a] is the fewest coins that add up to amount a, with fewest[0] = 0. The best way to make a ends with some coin c, and the coins before it must be the best way to make a - c, so fewest[a] = 1 + min(fewest[a - c]) over every coin c <= a. Fill the table from 1 up to amount, so every smaller amount is ready when you need it.
Brute force: Recursively try every coin as the next one without saving results, which takes exponential time.
Step by step
- 1Make
fewestof sizeamount + 1, withfewest[0] = 0and every other entry set toamount + 1, which stands for impossible. - 2For each amount
afrom 1 toamount, look at every coincwithc <= a. - 3If
fewest[a - c] + 1is smaller thanfewest[a], store it. - 4Return
fewest[amount], or -1 if it is stillamount + 1.
See the pattern move
The 1d dynamic programming pattern on a small example. Step through it, or press play.
Code
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
INF = amount + 1 # more coins than any real answer needs
# fewest[a] = fewest coins that add up to a
# fewest[a] = 1 + min(fewest[a - c]) over every coin c <= a
fewest = [0] + [INF] * amount
for a in range(1, amount + 1):
for c in coins:
if c <= a and fewest[a - c] + 1 < fewest[a]:
fewest[a] = fewest[a - c] + 1
return -1 if fewest[amount] == INF else fewest[amount]Complexity
Time
O(amount * k)
Space
O(amount)
For each amount from 1 to amount you look at each of the k coins once; the table has amount + 1 entries.
Watch out for
- Taking the largest coin first: with coins [1, 4, 5] and amount 8, greedy uses 4 coins, but 2 are enough.
- Using
Integer.MAX_VALUEas infinity and then adding 1 to it, which overflows;amount + 1is a safe stand-in. - Forgetting the -1 case: an entry still at the stand-in value means that amount can't be made.
Follow-up questions
How would you count the different combinations instead?
Add the counts instead of taking a minimum, and loop over coins in the outer loop so each combination is counted once. That is Coin Change II.
Could BFS solve it?
Yes. Treat each amount from 0 to amount as a node and each coin as an edge; the BFS level at which you first reach amount is the fewest coins.
In Hinglish
Har amount ke liye socho: aakhri coin kaunsa tha? Baaki amount ka best pehle se table mein hai, toh `fewest[a] = 1 + min(fewest[a - c])`.