← Whiteboard questions

Coin Change II

Count the different coin sets that make an exact total

DSA, Medium, 2-D DP

Which structure or algorithm, and what does it cost.

A shop has the coin values in the list coins, with as many coins of each value as you like. Count the different sets of coins that add up to exactly amount. Two sets are the same if they use each value the same number of times, in any order. Return 0 if no set works. Using no coins is the one set for an amount of 0.

Examples

Input coins = [3, 4, 6], amount = 12
Output 4
Why the sets are 3+3+3+3, 4+4+4, 6+6 and 3+3+6.
Input coins = [5, 7], amount = 3
Output 0
Why both coins are larger than the amount, so nothing fits.
Input coins = [4], amount = 0
Output 1
Why using no coins is the one set that adds up to 0.

Constraints

Your answer must contain

Target Aim for O(n * amount) time and O(amount) memory, where n is the number of coin values.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

If you count orders, 3+4 and 4+3 look different. How can you fix the order of the coins so each set is counted once?

Hint 2 the approach

Handle one coin value at a time. For each total, the sets that use this coin come from a smaller total.

Hint 3 the key step

Keep a list by total, with 1 at total 0. Put the coins in the outer loop. Which direction through the totals lets a coin repeat?

Walkthrough

Full walkthrough brute force to best

Brute force

Try every count of every coin that fits, and check the sum. The number of choices is the product of (amount / coin + 1) over the coins, about 3 * 10^20 for the coins 1 to 10 and amount 500, so this is out of reach.

Better method

Recursion with memory. Let ways(i, left) be the number of sets that use only coins from position i onward and add up to left. Either use coin i once more, which is ways(i, left - coin), or skip to the next coin, which is ways(i + 1, left). Store each pair. There are n * amount pairs, so this is O(n * amount) time and memory.

Best method

Keep a list ways, where ways[a] counts the sets that add up to a. Start with ways[0] = 1, the empty set. Take the coins one at a time, as the outer loop. For one coin c, go through the totals from c up to amount and add ways[a - c] to ways[a]. After this, ways[a] counts the sets of the coins seen so far, using any number of c. Going upward lets c be used again and again. Because the coins are the outer loop, each set is built in coin order only, so every set is counted once and not once per order. The answer is ways[amount].

Hand trace

Run on coins = [3, 4, 6], amount = 12. The list is ways[0] to ways[12], and starts as [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. After coin 3: [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]. After coin 4: [1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2]. After coin 6: [1, 0, 0, 1, 1, 0, 2, 1, 1, 2, 2, 1, 4]. The answer is ways[12], 4.

Code

def count_coin_sets(coins, amount):
    ways = [1] + [0] * amount        # ways[a]: sets of the coins seen so far that add up to a
    for c in coins:
        for a in range(c, amount + 1):
            ways[a] += ways[a - c]
    return ways[amount]

Time and memory

O(n * amount) time, one addition per coin and total. O(amount) memory for the list.

Common mistakes

  • Putting the totals as the outer loop. That counts 3+4+... in every order, and example 1 gives 6 instead of 4.
  • Starting ways[0] at 0. Every entry then stays 0, because nothing builds on the empty set.
  • Forgetting that an amount of 0 has one set, the empty one. Example 3 must give 1.

Related problems

More 2-D DP problems
Coin Change II: hints, walkthrough and Python solution