Coin Change II
Count the different combinations of coins that add up to an amount, counting each combination once.
The problem
You get coin values in coins, with an unlimited supply of each, and an amount. Return how many different combinations of coins add up to exactly amount, where the order of the coins doesn't matter. An amount of 0 has one combination, taking nothing, and you can assume the answer fits in a 32-bit integer.
Input amount = 6, coins = [1, 2, 4]
Output 6
1+1+1+1+1+1, 2+1+1+1+1, 2+2+1+1, 2+2+2, 4+1+1 and 4+2.
Input amount = 7, coins = [2, 4]
Output 0
Every mix of 2s and 4s adds up to an even number.
Key constraint: You count combinations, not sequences: 1 + 2 and 2 + 1 are the same combination.
How to spot the pattern
- You count the ways to reach a total with reusable items, and order doesn't matter.
- It is an unbounded knapsack: each coin can be used any number of times.
The idea
State: ways[i][a] is the number of combinations that make amount a using only the first i coin values. Either no more of coin i is used (ways[i - 1][a]) or at least one more is, which leaves a - coin to make with the same coins (ways[i][a - coin]), so ways[i][a] = ways[i - 1][a] + ways[i][a - coin]. Keeping one row and looping over coins in the outer loop computes exactly this, and since coins are added in a fixed order, each combination is counted once.
Brute force: Recursively choose how many of each coin to take, which takes exponential time.
Step by step
- 1Make
waysof sizeamount + 1withways[0] = 1. - 2For each coin, in the outer loop, go through every amount
afromcoinup toamount. - 3Add
ways[a - coin]toways[a]; going upward lets the same coin be used again. - 4Return
ways[amount].
See the pattern move
The 2d dynamic programming pattern on a small example. Step through it, or press play.
Code
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
# ways[i][a] = ways to make a with the first i coins
# = ways[i - 1][a] (no more of coin i) + ways[i][a - coin] (one more coin i)
# One row is enough; looping over coins outside counts each combination once.
ways = [0] * (amount + 1)
ways[0] = 1 # one way to make 0: take nothing
for coin in coins:
for a in range(coin, amount + 1):
ways[a] += ways[a - coin]
return ways[amount]Complexity
Time
O(k * amount)
Space
O(amount)
Each of the k coins sweeps the table of amount + 1 entries once.
Watch out for
- Putting amounts in the outer loop and coins inside: that counts orders, so 1 + 2 and 2 + 1 become two ways.
- Looping the amounts downward, which allows each coin only once (that is the 0/1 knapsack of Partition Equal Subset Sum).
- In C++, an
inttable: entries for other amounts can overflow even when the answer fits, which is undefined behavior, while unsigned numbers wrap safely.
Follow-up questions
What if the order of the coins mattered?
Swap the loops, amounts outside and coins inside. Then ways[a] counts sequences, as in Combination Sum IV.
How is this different from Coin Change?
Coin Change takes a minimum over the last coin used; here you add counts, and the loop order decides whether you count combinations or sequences.
In Hinglish
Coins ko bahar wale loop mein rakho, tabhi har combination ek hi baar ginega. `ways[a] += ways[a - coin]`, aur amount upar ki taraf chalao taaki coin dobara use ho sake.