Browse curriculum

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.

  1. dp[0] = 0; every other amount starts at ∞ (not reachable yet).
  2. Fill dp[1], dp[2], … dp[amount] in order, so dp[a - c] is always already known.
  3. Return dp[amount], or -1 if 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

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

Free preview: the first 11 of 35 steps

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 -1
Coin 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, check dp[a - coin] != INT_MAX before adding 1.
  • Greedy: largest-coin-first is not optimal for every coin set ([1, 3, 4], amount 6).
  • 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

The approach, step by step

  1. Set up the table

    dp[0] = 0; every other amount starts at infinity.

  2. Try every coin as the last one

    For each amount a and coin c <= a, set dp[a] = min(dp[a], dp[a - c] + 1).

  3. Read the answer

    Return dp[amount], or -1 if it is still infinity.

Frequently asked questions

What is the recurrence for Coin Change?

dp[a] is the fewest coins that make amount a. Try each coin as the last one used: dp[a] = min over coins with coin <= a of dp[a - coin] + 1. dp[0] = 0, and an amount that no combination reaches stays at infinity, which becomes -1.


Why doesn't a greedy approach work for Coin Change?

Always taking the largest coin that fits is only optimal for some coin systems. With coins [1, 3, 4] and amount 6, greedy takes 4 + 1 + 1, three coins, but 3 + 3 uses two. The DP tries every last coin, so it never commits to a bad first choice.


What is the time and space complexity of Coin Change?

O(amount × k) time for k coin types, since every amount tries every coin, and O(amount) space for the dp array.


Why initialize the DP array with amount + 1 instead of Integer.MAX_VALUE?

Initializing with amount + 1 avoids integer overflow when adding 1 in dp[a - coin] + 1. Since the worst-case number of coins is amount (all 1s), amount + 1 acts as a safe infinity sentinel.


What is the difference between Coin Change and Coin Change II?

Coin Change (LeetCode 322) finds the minimum number of coins to reach the amount (optimization problem). Coin Change II (LeetCode 518) counts the total number of distinct combinations (counting problem).

New to the technique behind this problem? Watch it run step by step:

Dynamic Programming Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description