Home
DSA Patterns
Dynamic Programming
Coin Change
Coin Change (LeetCode 322)
Coin Change (LeetCode 322): find the fewest coins that make up an amount, or -1 if it cannot be made, with a bottom-up DP over amounts.
Problem Understanding
Coin Change: given coin denominations coins and a target amount, return the fewest coins that add up to amount, using each denomination as many times as you like. If no combination works, return -1.
Examples: coins = [1, 2, 5], amount = 11 returns 3 (5 + 5 + 1). coins = [2], amount = 3 returns -1.
Attempt 1: Greedy, Then Brute Force
Taking the largest coin that fits looks right for [1, 2, 5], but it fails in general: for [1, 3, 4] and amount 6, greedy picks 4 + 1 + 1 (three coins) while 3 + 3 uses two. Trying every combination recursively is correct but exponential, and it recomputes the same remaining amounts again and again, which is exactly what a DP table removes.
The Intuition: Which Coin Was Last?
Any way of making amount a ends with some last coin c. Before it, you had to make a - c: as cheaply as possible. So:
dp[a] = min(dp[a - c] + 1) over every coin c ≤ a.
dp[0] = 0; every other amount starts at ∞ (not reachable yet).- Fill
dp[1],dp[2], …dp[amount]in order, sodp[a - c]is always already known. - Return
dp[amount], or-1if it is still ∞.
Trying every coin as the last one is what makes this correct where greedy is not.
Coin Change Visualization
coins [1, 2, 5], amount 11 · dp[a] = FEWEST COINS FOR a
BASE CASE
dp
0
1
2
3
4
5
6
7
8
9
10
11
Amount
:
Last coin
:
Answer
:
dp[0] = 0: zero coins make amount 0. Every other amount starts at ∞ (not yet reachable)
Next
Amount 1, last coin 1: dp[0] + 1 = 1, better. dp[1] = 1
Coins
Target
The dp table runs left to right. For each amount, every coin is tried in turn, and the cell it reads: dp[a − coin]: is marked; when that gives fewer coins, the new count is written. The last step traces the answer's coins back through the table.
Coin Change Dry Run
Input: coins = [1, 2, 5], amount = 11
a |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
dp[a] |
0 | 1 | 1 | 2 | 2 | 1 | 2 | 2 | 3 | 3 | 2 | 3 |
For example, dp[11] tries dp[10] + 1 = 3 (last coin 1), dp[9] + 1 = 4 (last coin 2) and dp[6] + 1 = 3 (last coin 5): the minimum is 3, e.g. 5 + 5 + 1.
Coin Change LeetCode Solution
Bottom-up DP over amounts, trying every coin as the last one.
Coin Change Code
1var coinChange = function(coins, amount) {2 const dp = new Array(amount + 1).fill(Infinity);3 dp[0] = 0;4 for (let a = 1; a <= amount; a++) {5 for (const coin of coins) {6 if (coin <= a && dp[a - coin] + 1 < dp[a]) {7 dp[a] = dp[a - coin] + 1;8 }9 }10 }11 return dp[amount] === Infinity ? -1 : dp[amount];12};
Coin Change in Python, Java, C++, TypeScript and C
Coin Change in Python
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [float('inf')] * amount
for a in range(1, amount + 1):
for coin in coins:
if coin <= a and dp[a - coin] + 1 < dp[a]:
dp[a] = dp[a - coin] + 1
return dp[amount] if dp[amount] != float('inf') else -1Coin Change in Java
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int a = 1; a <= amount; a++) {
for (int coin : coins) {
if (coin <= a && dp[a - coin] != Integer.MAX_VALUE && dp[a - coin] + 1 < dp[a]) {
dp[a] = dp[a - coin] + 1;
}
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}Coin Change in C++
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int a = 1; a <= amount; a++) {
for (int coin : coins) {
if (coin <= a && dp[a - coin] != INT_MAX && dp[a - coin] + 1 < dp[a]) {
dp[a] = dp[a - coin] + 1;
}
}
}
return dp[amount] == INT_MAX ? -1 : dp[amount];
}
};Coin Change in TypeScript
function coinChange(coins: number[], amount: number): number {
const dp: number[] = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let a = 1; a <= amount; a++) {
for (const coin of coins) {
if (coin <= a && dp[a - coin] + 1 < dp[a]) {
dp[a] = dp[a - coin] + 1;
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}Coin Change in C
int coinChange(int* coins, int coinsSize, int amount) {
int *dp = malloc((amount + 1) * sizeof(int));
dp[0] = 0;
for (int a = 1; a <= amount; a++) dp[a] = INT_MAX;
for (int a = 1; a <= amount; a++) {
for (int j = 0; j < coinsSize; j++) {
int coin = coins[j];
if (coin <= a && dp[a - coin] != INT_MAX && dp[a - coin] + 1 < dp[a]) {
dp[a] = dp[a - coin] + 1;
}
}
}
int best = dp[amount] == INT_MAX ? -1 : dp[amount];
free(dp);
return best;
}Edge Cases & Common Mistakes
- Amount 0: return
0: no coins needed. - Unreachable amount:
dp[amount]stays ∞; return-1, not ∞ or a sentinel value. - Overflow with
INT_MAX: in Java, C++ and C, checkdp[a - coin] != INT_MAXbefore adding 1. - Greedy: largest-coin-first is not optimal for every coin set (
[1, 3, 4], amount6). - Counting combinations instead: "how many ways" is Coin Change II: it sums instead of minimising, and loops coins on the outside.
Summary
Ask which coin was used last: dp[a] = min(dp[a - c] + 1). Filling amounts in increasing order makes every dependency ready in time. O(amount × k) time, O(amount) space.
More practice: DSA interview questions with answers · patterns cheat sheet
