You are given a set of coin denominations and a total amount. Write a function to compute the number of combinations that make up that amount. Please this is the "Coin Change 2" problem, this is a classic dynamic programming problem, write a function that solves this problem, and explain your DP state and recurrence relation.
Assistant
We are given the classic "Coin Change 2" problem: Given an integer array coins representing coins of different denominations and an integer amount, return the number of combinations that make up that amount. We assume we have an infinite number of each coin. The order of coins does not matter, so we count combinations, not permutations.
We need to write a function and explain the DP state and recurrence.
Dynamic Programming approach:
Let dp[i] be the number of ways to make amount i using the given coin denominations (considering combinations, order doesn't matter). We want dp[amount].
We initialize dp[0] = 1 because there is exactly one way to make amount 0: using no coins.
We iterate over each coin and update the dp array. Specifically, for each coin c in coins, we iterate from amount = c to target amount, and do:
dp[amount] += dp[amount - c]
Why? Because when we consider coin c, we are adding the number of ways to make (amount - c) to the current amount's ways. Since we iterate coins one by one and update dp in increasing amount order for each coin, we ensure that combinations are counted without regard to order. That is, we are essentially building combinations by considering each coin and then all amounts that can be formed by including that coin.
Alternatively, we can also use a 2D DP: dp[i][j] = number of ways to make amount j using the first i coins. But the 1D version is more space efficient.
Recurrence for 2D:
dp[0][0] = 1, dp[0][j>0] = 0.
For i from 1 to n (n = number of coins):
for j from 0 to amount:
dp[i][j] = dp[i-1][j] + (if j >= coins[i-1] then dp[i][j - coins[i-1]] else 0)
The term dp[i-1][j] means we don't use the i-th coin, and dp[i][j - coins[i-1]] means we use at least one of the i-th coin. But note: in the 2D version, the second term uses dp[i][...] because we are allowed to use the same coin multiple times. That is why it's dp[i][j - coin] not dp[i-1][...]. So recurrence:
dp[i][j] = dp[i-1][j] + dp[i][j - coins[i-1]] (if j >= coins[i-1])
But we can optimize to 1D by noticing that we only need the previous row? Actually, for each coin, we are updating the same row because we are allowed to use multiple of the same coin. So we do:
Initialize dp[0] = 1, others 0.
For each coin c in coins:
for a from c to amount:
dp[a] += dp[a-c]
This works because when processing coin c, dp[a] already contains the number of ways using coins before c (i.e., coins with index less than current). Then we add the ways that use at least one c, which is dp[a-c] (but note: dp[a-c] might already include some c? Actually, since we are iterating a from c to amount, when we compute dp[a] we are adding dp[a-c] which at that point already includes ways that use c? Let's see: For a given coin c, we iterate a from c to amount. dp[a-c] might have been updated in the same loop? For example, when a = c, dp[c] += dp[0] = 1. Then when a = 2c, dp[2c] += dp[c] which now includes the way of using one c? So that counts using two c's? Yes. So it correctly builds up using multiple c's. And because we are iterating over coins one by one, we don't count permutations. For example, if we have coins [1,2] and amount 3, we first process coin 1: dp becomes [1,1,1,1]. Then process coin 2: for a=2, dp[2] += dp[0] -> dp[2]=1+1=2? Wait: initially after coin1, dp[2]=1. Then for coin2, when a=2, dp[2] += dp[0] =1, so dp[2] becomes 2. That corresponds to ways: using two 1's or one 2. Then for a=3, dp[3] += dp[1] =1, so dp[3] becomes 1+1=2? Initially after coin1, dp[3]=1. Then dp[3] += dp[1]=1, so dp[3]=2. That corresponds to: three 1's and one 1+one 2. So total 2. That's correct. So the order of coins doesn't matter and we don't count (1,2) and (2,1) as separate because we are processing coin 1 first and then coin 2, so when we add coin 2, we only consider combinations that include coin 2 and possibly previous coins. But note: when we update dp[3] using dp[1] from the same coin2 loop? Actually, dp[1] is from after processing coin1 only because we haven't updated dp[1] in the coin2 loop (since a starts at 2). So dp[1] remains 1 from coin1. So we are adding one 2 and then the rest from coin1. That gives combination of 1+2. And we don't later get 2+1 because we already processed coin1 earlier and when processing coin2 we only consider adding coin2 to amounts that were formed using coins up to coin1. So no duplication.
Thus the 1D DP is correct.
We need to be careful about integer overflow? The problem might ask for modulo? Usually in LeetCode it's just return the number, but it can be large. We'll assume we return an integer.
Let's implement in Python.
Function signature: def change(amount: int, coins: List[int]) -> int:
We'll do:
if amount == 0: return 1? Actually, dp[0]=1 always.
But if amount is 0, there is one way: no coins. So that's fine.
Edge: if amount < 0? Not possible.
We'll create dp array of size amount+1, all zeros, dp[0]=1.
Then for each coin in coins:
for a from coin to amount:
dp[a] += dp[a-coin]
Return dp[amount].
But we must consider that coins might not be sorted? It doesn't matter because we process in given order, but the result should be the same regardless of order? Actually, the order of processing coins does not affect the final count because we are essentially building combinations by considering each coin exactly once. But the algorithm works for any order. So we don't need to sort.
However, if we sort, it might be more cache friendly? Not necessary.
Now, explanation:
DP state: dp[i] represents the number of ways to make amount i using the coins processed so far.
Recurrence: When we consider a new coin c, for each amount a from c to target, we add the number of ways to make (a-c) to dp[a]. Because for each way to make (a-c), we can add one coin c to make amount a. Since we are processing coins one by one, we avoid counting permutations.
Time complexity: O(n * amount), where n is number of coins.
Space complexity: O(amount).
Let's write code accordingly.
We use dynamic programming to count the number of combinations (order-independent) to form the target amount. The key insight is to process each coin denomination sequentially and update a 1‑dimensional DP array where dp[a] represents the number of ways to form amount a using the coins considered so far.
DP State dp[a] = number of combinations to make amount a with the coins processed up to the current point.
Recurrence Relation
For each coin c, iterate a from c to amount and update: dp[a] += dp[a - c]
This means that for every way to form a - c, adding one coin c yields a valid way to form a. Since we process coins one after another, each combination is counted exactly once regardless of the order of coins.
Initialization dp[0] = 1 because there is exactly one way to make amount 0: using no coins. All other dp[a] start at 0.
Algorithm
Create dp array of length amount + 1 initialized to 0, set dp[0] = 1.
For each coin c in coins:
For a from c to amount: dp[a] += dp[a - c]
Return dp[amount].
Complexity
Time: O(n * amount), where n is the number of coins.
Space: O(amount).
python
def change(amount: int, coins: List[int]) -> int:
dp = [0] * (amount + 1)
dp[0] = 1 # one way to make zero amount
for c in coins:
for a in range(c, amount + 1):
dp[a] += dp[a - c]
return dp[amount]
This solution efficiently counts all unique combinations without repetition, leveraging the 1‑D DP optimization.