Coin change
Standard · Write code · about 15 min
coins holds coin values; you have unlimited coins of each value. Return the fewest coins that add up to exactly amount, or -1 if no mix of coins does.
Example 1
Input:
[[1,2,5],11]
Output:
3
5 + 5 + 1.
Example 2
Input:
[[2],3]
Output:
-1
Only even amounts can be made.
Constraints
- 1 ≤ coins.length ≤ 1000
- 1 ≤ coins[i] ≤ 1000
- 0 ≤ amount ≤ 1000
Learn the idea first: Coin change: minimum coins and number of ways, a visual lesson.
More Coin change: minimum coins and number of ways problems
- Fill the coin table Warm-up
- Coin change II Standard
- Perfect squares Standard
- Combination sum IV Challenge