LeetCode 322 · Medium

Coin Change Explained: Fewest Coins (LeetCode 322)

Updated Nov 27, 2026 · 3 min read

Build a table where dp[x] is the fewest coins that make amount x. Set dp[0] = 0, then for each amount try every coin c as the last one: dp[x] = min(dp[x - c] + 1). Return dp[amount], or -1 if it was never reached. It runs in O(amount × coins) time and O(amount) space.

Watch onYouTubeInstagram

Key points

  • ●Grabbing the biggest coin first can fail: with 1, 3 and 4, greedy pays 6 with three coins, but 3 + 3 uses two.
  • ●Every combination has a last coin, so dp[x] = min over coins of dp[x - c] + 1.
  • ●Fill the table from 0 upward so every smaller answer is ready.
  • ●Use amount + 1 as an 'impossible' marker and return -1 if it's still there.
Step-by-step visual sheet: Coin Change
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: the cashier got it wrong

Kai's shop has coins worth 1, 3 and 4. A customer needs 6 cents in change. The cashier grabs the biggest coin first: 4, then 1, then 1. Three coins.

But 3 + 3 is also 6, and that's two coins.

That's LeetCode 322, Coin Change. You get coin values, with as many of each as you like, and an amount. Return the fewest coins that add up to it exactly, or -1 if no mix can.

The slow way: try every mix

Greedy is fast, but as Kai's cashier shows, it isn't always right. The safe brute force tries every possible sequence of coins and keeps the shortest. It branches on every coin choice and keeps re-solving the same smaller amounts, so it blows up fast.

The idea: fewest coins for every amount

Here's the human way. Kai writes down the fewest coins for every amount from 0 up to the target.

The key observation is that every valid combination has some last coin c. Take it away and you're left with amount x - c, which you've already solved. So:

  • dp[0] = 0, zero coins make zero.
  • dp[x] = min(dp[x - c] + 1) over every coin c that fits.

For Kai's 6 cents: 6 minus 3 is 3, and 3 takes one coin, so 6 takes two. Greedy never looked at that branch.

Trace it: 11 cents with 1, 2 and 5

The visual sheet above works the example coins = [1, 2, 5] and amount = 11. Here's the full table:

Amount 0 1 2 3 4 5 6 7 8 9 10 11
Fewest coins 0 1 1 2 2 1 2 2 3 3 2 3

A few rows worth checking by hand:

  • dp[5] = dp[0] + 1 = 1, a single 5.
  • dp[6] = dp[1] + 1 = 2, a 5 and a 1.
  • dp[10] = dp[5] + 1 = 2, two 5s.
  • dp[11] = dp[6] + 1 = 3, so the answer is 5 + 5 + 1.

Step 4 of the sheet fills this table, and step 6 traces the last transition.

The code in Python

def coin_change(coins, amount):
    impossible = amount + 1
    dp = [impossible] * (amount + 1)
    dp[0] = 0
    for current in range(1, amount + 1):
        for coin in coins:
            if coin <= current:
                dp[current] = min(
                    dp[current],
                    dp[current - coin] + 1
                )
    return -1 if dp[amount] == impossible else dp[amount]

The outer loop picks the amount, the inner loop tries each coin as the last one. Run it on coins = [1, 2, 5], amount = 11 and it returns 3. With coins = [1, 3, 4], amount = 6 it returns 2, beating the cashier.

Complexity

  • Time: O(amount × number of coins). Each amount tries every coin once.
  • Space: O(amount) for the table.

Brute force is exponential. The table turns it into a simple double loop.

The trap to watch for

Two of them.

First, the "impossible" marker. If you fill the table with 0 or skip the check, unreachable amounts look like free answers. With coins = [2] and amount = 3, the right answer is -1. Starting every cell at amount + 1 and checking for it at the end handles that, which is what the sheet's step 7 does.

Second, trusting greedy because it works on everyday coins. It happens to work for some coin sets, but the problem gives you any coins, and 1, 3, 4 is all it takes to break it.

Frequently asked questions

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

Taking the biggest coin first only works for some coin systems. With coins 1, 3 and 4 and an amount of 6, greedy takes 4, then 1, then 1, which is three coins. The best answer is 3 + 3, two coins.

What is the time complexity of Coin Change?

The bottom-up dynamic programming solution is O(amount × number of coins) time, because every amount tries every coin, and O(amount) space for the table.

Why use amount + 1 as the starting value?

No real answer can need more than amount coins, since even all 1-coins would only use amount of them. So amount + 1 safely means 'not reachable yet', and if dp[amount] still holds it at the end you return -1.

Is Coin Change a knapsack problem?

Yes, it's the unbounded (complete) knapsack shape: each coin can be used any number of times, and you're minimizing the count instead of maximizing a value.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free