Back to Practice
#0066
Coin Change Minimum Coins
MediumDSA30 min20 XP
Problem
Given coin denominations and an amount, return the minimum number of coins needed to make that amount. Return -1 if impossible.
Why This Matters
This is the standard unbounded knapsack pattern: reuse previous answers to build larger amounts.
Function Signature
def coin_change(coins, amount):
Examples
Example 1
Inputcoins = [1, 2, 5], amount = 11
Output3
11 can be made as 5 + 5 + 1.
Constraints
- Coins can be reused.
- Return 0 when amount is 0.
- Return -1 when impossible.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Reusable coins
Input[1,2,5], 11
Expected3
Two 5s and one 1 use three coins.
Impossible amount
Input[2], 3
Expected-1
Odd amount cannot be made from coin 2.
Hidden Test Categories
amount zerosingle exact coingreedy would fail