Coin Change 2
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the number of combinations that make up that amount.
Examples
amount = 5, coins = [1,2,5]
4
There are 4 combinations: 5=5, 5=2+2+1, 5=2+1+1+1, 5=1+1+1+1+1.
amount = 3, coins = [2]
0
There is no combination of coins that sums to 3.
Constraints
- •
1 <= coins.length <= 300 - •
1 <= coins[i] <= 5000 - •
All the values of coins are unique. - •
0 <= amount <= 5000
Approaches
Try all combinations of coins.
def change(amount, coins):
def helper(i, remaining):
if remaining == 0:
return 1
if remaining < 0 or i == len(coins):
return 0
return helper(i, remaining - coins[i]) + helper(i + 1, remaining)
return helper(0, amount)Use DP where dp[i] is the number of ways to make amount i.
def change(amount, coins):
dp = [0] * (amount + 1)
dp[0] = 1
for coin in coins:
for i in range(coin, amount + 1):
dp[i] += dp[i - coin]
return dp[amount]Same DP with careful iteration order to avoid counting permutations.
Diagram
def change(amount, coins):
dp = [0] * (amount + 1)
dp[0] = 1
for coin in coins:
for j in range(coin, amount + 1):
dp[j] += dp[j - coin]
return dp[amount]Complexity Comparison
| Approach | Time | Space | Description |
|---|---|---|---|
| Recursion | O(2^n) | O(amount) | Try all combinations of coins. |
| DP - Bottom Up | O(amount * len(coins)) | O(amount) | Use DP where dp[i] is the number of ways to make amount i. |
| Optimized DP | O(amount * len(coins)) | O(amount) | Same DP with careful iteration order to avoid counting permutations. |
Try all combinations of coins.
Use DP where dp[i] is the number of ways to make amount i.
Same DP with careful iteration order to avoid counting permutations.
Common Mistakes
Counting permutations instead of combinations (wrong iteration order)
Not initializing dp[0] = 1
Using 2D DP when 1D is sufficient