Dynamic programming · Algorithm 14 of 15
Minimum Coin Change
Find the fewest coins even when a greedy choice fails. Citation needed
The whole, before the parts
The coin-change problem asks for the fewest coins that make an exact amount, given allowed coin values. Solving smaller amounts first lets the complete solution reuse earlier answers. Citation needed
Terminology and core concepts
Given positive integer coin denominations and an unlimited supply of each, find the minimum number of coins for an exact non-negative integer amount. Define best[a] as the fewest coins that make amount a. If the last coin has value c, the rest must make a - c, giving candidate best[a - c] + 1. Trying every possible last coin makes the recurrence complete. Zero needs no coins; an unreachable amount starts at infinity. Citation needed
Learning path
Trace an example
coins [1, 3, 4] amount 0 1 2 3 4 5 6 fewest coins 0 1 2 1 1 2 2 for amount 6: last 1: best[5] + 1 = 3 last 3: best[3] + 1 = 2 (choose 3 + 3) last 4: best[2] + 1 = 3
Implement it in Python
Read the loop alongside the trace, then run this self-contained example with Python 3. The assertions show expected results. Inputs use ordinary Python lists and dictionaries; comparisons and numeric operations are assumed to behave normally. Citation needed
def min_coins(coins, amount):
if not isinstance(amount, int) or amount < 0:
raise ValueError('amount must be a non-negative integer')
if any(not isinstance(c, int) or c <= 0 for c in coins):
raise ValueError('coin values must be positive integers')
best = [float('inf')] * (amount + 1)
best[0] = 0
for total in range(1, amount + 1):
for coin in coins:
if coin <= total:
best[total] = min(best[total], best[total - coin] + 1)
return -1 if best[amount] == float('inf') else best[amount]
assert min_coins([1, 3, 4], 6) == 2
assert min_coins([2], 3) == -1
assert min_coins([], 0) == 0Citation needed
Analyze the cost
For k denominations and amount A, there are A states with k choices each: O(kA) time and O(A) space, apart from input validation. This is pseudo-polynomial: it depends on the numeric amount, which can be much larger than its digit count. Citation needed
A mistake to watch for
This returns the count, not the actual coins. To reconstruct a solution, store which last coin achieved each best value. Zero or negative coin values break the smaller-subproblem reasoning. Citation needed
Practice and recall
-
For coins [2, 5], what does amount 6 return?
Work it out before opening the solution. Citation needed
Show the reasoning
- Amount 4 is reachable with two coins of value 2.
- Ending with 2 gives best[4] + 1 = 3; ending with 5 leaves unreachable amount 1.
- The minimum is three coins: 2 + 2 + 2.
Citation needed
- What does best[0] equal?
- Zero: making amount zero needs no coins. Citation needed
To check your understanding, reproduce the implementation without looking, explain why its main step is safe, and test an edge case before moving to the next algorithm. Citation needed
Sources
- MIT OpenCourseWare: Introduction to Algorithms — lecture notes — further study of the underlying algorithm and analysis.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms — correctness arguments and complexity analysis.