Coding interview problems › Dynamic Programming
🪙 Dynamic Programming Medium
Coin Change II (Number of Ways)
Coin Change II explained: count the combinations of coins that make an amount, why the loop order matters, O(n·amount) DP in tested Python, with a live visual.
Also searched as: coin change number of ways · count combinations of coins to make amount · unbounded knapsack counting
Try it live Free
Coin Change (DP)
In the Coin Change lab, watch dp[] fill left to right with each cell built from earlier cells; here each cell adds earlier counts instead of taking a minimum.
Also useful: 0/1 Knapsack (DP)
The problem
Given coin denominations (unlimited supply of each) and an amount, count how many different combinations of coins add up to it. Order does not matter: 1+2 and 2+1 are the same combination.
5, 2+2+1, 2+1+1+1, 1+1+1+1+1
The key insight
To count combinations rather than orderings, decide coins one denomination at a time. Process coin c and update ways[a] += ways[a - c] for every amount. Because each coin is fully processed before the next, every combination is built in one fixed coin order, so it is counted exactly once.
Step-by-step approach
- Create
waysof sizeamount + 1withways[0] = 1(one way to make 0: use nothing). - Loop over coins in the outer loop.
- For each coin
c, loopafromcup toamountand addways[a - c]toways[a]. - Going upward lets the same coin be reused any number of times.
- Return
ways[amount].
Python solution
def change(amount, coins):
ways = [1] + [0] * amount # ways[a] = combinations making a
for c in coins: # coins OUTSIDE: counts combinations
for a in range(c, amount + 1):
ways[a] += ways[a - c]
return ways[amount]Complexity
Each of the n coins sweeps the table of amount + 1 cells once.
Common mistakes
- Swapping the loops (amounts outside, coins inside). That counts ordered sequences, so 1+2 and 2+1 are counted separately and the answer is too big.
- Setting
ways[0] = 0. With no base way, every cell stays 0. - Iterating amounts downward, which turns it into 0/1 knapsack where each coin can be used at most once.
Frequently asked questions
What is the time and space complexity of Coin Change II?
O(number of coins × amount) time and O(amount) space with the 1D table.
Why does the loop order matter in Coin Change II?
With coins in the outer loop, each combination is assembled in a fixed coin order and counted once. With amounts in the outer loop, every ordering of the same coins is counted, which answers a different question (number of ordered sequences).
How is this related to unbounded knapsack?
It is the counting version of unbounded knapsack: each item (coin) can be used any number of times, which is why the inner loop runs upward.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Dynamic Programming Coin Change (DP) lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.