← DSA
MEDIUM

Coin Change II

PracticeNeetCode ↗LeetCode ↗

Given a target amount and an array of coin denominations (unlimited supply of each), count how many distinct combinations of coins add up to the amount. Order doesn't matter — [1,2] and [2,1] are the same combination.

Example

Input amount = 5, coins = [1,2,5]
Output 4

Key Pointers

Step-by-Step Analysis

  1. Create a dp array of size amount + 1, all zeros, with dp[0] = 1.
  2. For each coin denomination, walk x from that coin's value up to amount.
  3. At each x, add dp[x - coin] into dp[x] — the number of ways to make x - coin becomes the number of new ways to make x using one more of this coin.
  4. Finishing one coin fully before starting the next is what prevents counting the same combination in multiple orders.
  5. Return dp[amount].

Complexity

Time: O(amount · len(coins)) — Space: O(amount)

Code

def change(amount: int, coins: list[int]) -> int:
    dp = [0] * (amount + 1)
    dp[0] = 1

    for coin in coins:
        for x in range(coin, amount + 1):
            dp[x] += dp[x - coin]

    return dp[amount]

Code Explanation

How the solution above works, piece by piece.

  1. dp[x] = number of ways to make amount x; dp[0] = 1 (use no coins).
  2. The outer loop is over coins, so combinations are built using coin types in a fixed order. This counts combinations, not permutations.
  3. For each amount x ≥ coin, dp[x] += dp[x - coin]: every way to make x − coin becomes a way to make x by adding this coin.
  4. Looping x upward lets the same coin be reused (the updated dp[x - coin] may already include this coin).
  5. O(amount × coins) time, O(amount) space.

Visual Walkthrough

Running it on amount = 5, coins = [1, 2, 5]:

Coin change II table after adding each coin012345start100000coin 1111111coins 1,2112233coins 1,2,5112234dp[x] = ways to make xwith the coins used so faradding a coin: dp[x] += dp[x − coin]5 = 5 | 2+2+1 | 2+1+1+1 | 1×5
Coins are added one type at a time. Looping over coins in the outer loop means each combination is counted once in a fixed order (all 1s, then 2s, then 5s), so 1 + 2 and 2 + 1 aren't counted twice. Result: 4 ways.
answer

Video Explanation

NeetCode — Coin Change II, Unbounded Knapsack, LeetCode 518