Coin Change II
Medium
Topics
You are given coin denominations nums and a target amount k. Return the number of distinct combinations that make up the amount (order does not matter). Return 0 if impossible.
Example 1
coins
answer
Each chip is one way to make 5, and a coin may be used any number of times. What counts as one way is the multiset, not the order —
1 + 2 and 2 + 1 are the same answer, which is the distinction the whole problem turns on. There are 4.Chips are the denominations available; below them every distinct combination.
Input: nums = [1,2,5], k = 5 Output: 4
Example 2
coins
answer
Input: nums = [2], k = 3 Output: 0
Constraints
- 1 <= nums.length <= 300
- 1 <= nums[i] <= 5000
- 0 <= k <= 5000
Editorial — intuition, approach, complexity and pitfalls
Intuition
To count combinations (not permutations), iterate coins in the outer loop so each coin is considered once — this prevents counting the same multiset in different orders.
Approach
- dp[a] = number of ways to make amount a; dp[0] = 1.
- For each coin, for each amount a from coin to k: dp[a] += dp[a-coin].
- The coin-outer ordering is what enforces combinations.
Dry Run
coins [1, 2, 5] amount 5
dp[a] = number of COMBINATIONS summing to a. dp[0] = 1: one way to make nothing.
start [1, 0, 0, 0, 0, 0]
after coin 1 [1, 1, 1, 1, 1, 1] only 1s: exactly one way per amount
after coin 2 [1, 1, 2, 2, 3, 3] now 2s may be added to any of those
after coin 5 [1, 1, 2, 2, 3, 4]
answer 4 -- 5, 2+2+1, 2+1+1+1, 1+1+1+1+1
The loop order is the whole algorithm. Coins outer means that when coin 2 is being
swept, every entry of dp already represents sets drawn only from {1}; adding 2s on
top can therefore never produce a set that was already counted, because no earlier
set contained a 2. Each combination is built in exactly one order: coins in the
order the outer loop visits them.
Swap the loops so amount is outer and the answer becomes 9 instead of 4. That
counts ORDERED sequences -- 1+2+2, 2+1+2 and 2+2+1 become three answers rather
than one -- which is a different and also useful quantity, just not this question.
dp[0] = 1 is load-bearing rather than cosmetic. Every count in the table
ultimately traces back to it; seed it with 0 and the whole array stays 0.Complexity
- Time — O(coins*amount): each coin sweeps the amount array.
- Space — O(amount): the 1-D dp array.
Common Pitfalls
- Coins outer, amount inner — swapping the loops counts ordered permutations instead.
Full Solution — all languages
class Solution:
def change(self, nums, k):
dp=[0]*(k+1); dp[0]=1
for coin in nums:
for a in range(coin,k+1):
dp[a]+=dp[a-coin]
return dp[k]import java.util.*;
class Solution {
public int change(int[] nums, int k) {
long[] dp=new long[k+1];
dp[0]=1;
for(int coin:nums)for(int a=coin;a<=k;a++)dp[a]+=dp[a-coin];
return (int)dp[k];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int change(vector<int>& nums, int k) {
vector<long long> dp(k+1,0);
dp[0]=1;
for(int coin:nums)for(int a=coin;a<=k;a++)dp[a]+=dp[a-coin];
return (int)dp[k];
}
};/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/ var change = function(nums, k) {
const dp=new Array(k+1).fill(0);
dp[0]=1;
for(const coin of nums)for(let a=coin;a<=k;a++)dp[a]+=dp[a-coin];
return dp[k];
};
Asked at
D. E. Shaw