← Whiteboard questions

Coin Change

Find the fewest coins that make an amount

DSA, Medium, 1-D DP

Which structure or algorithm, and what does it cost.

You have an unlimited supply of coins of some values, for example 1, 5 and 12. You get a list coins of the coin values and a whole number amount. Return the smallest number of coins that add up to exactly amount. If the coins cannot make the amount at all, return -1. If amount is 0, return 0.

Examples

Input coins = [1, 5, 12], amount = 15
Output 3
Why 5 + 5 + 5. Taking the biggest coin first (12 + 1 + 1 + 1) needs 4 coins.
Input coins = [4, 6], amount = 7
Output -1
Why Every sum of 4s and 6s is even, so 7 cannot be made.
Input coins = [3], amount = 0
Output 0
Why Edge case: the empty set of coins already makes 0.

Constraints

Your answer must contain

Target Aim for O(amount x coins) time and O(amount) memory.

Hints

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

Hint 1 a nudge

Take the biggest coin first with coins 1, 5 and 12 and amount 15. Does that give the fewest coins? Look for an amount where it does not.

Hint 2 the approach

Let best of a be the fewest coins that make amount a. Think about the last coin you use. Which smaller amount is left?

Hint 3 the key step

Fill a table from amount 0 upwards. For each amount, try every coin that fits and take the smallest table value of the amount left, plus 1. Mark impossible amounts.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every combination of coins that adds up to the amount, with recursion, and keep the smallest count. Each step branches over all the coins, so the work grows exponentially. For amounts up to 20,000 it never finishes.

Best method

Let best[a] be the fewest coins that make amount a, and let best[0] be 0. For a above 0, the last coin is some coin c that is at most a, so best[a] is 1 plus best[a - c], and you take the smallest over all coins. The smaller problem has the same form, and the best way to make a - c does not depend on how it is used later, so the rule is correct. Fill the table from 1 up to the amount. Start every cell at amount + 1, a value larger than any real answer, to mean cannot be made. The greedy method, largest coin first, is not correct: with coins 1, 5 and 12 and amount 15 it takes 12 + 1 + 1 + 1 (four coins), but 5 + 5 + 5 needs three.

Hand trace

Run on coins [1, 5, 12] and amount 15. The table starts with 0 at amount 0. Amounts 1 to 4 need that many 1-coins, so they hold 1, 2, 3, 4. Amount 5 holds 1. Amounts 6 to 9 hold 2, 3, 4, 5. Amount 10 holds 2, amount 11 holds 3, amount 12 holds 1, amount 13 holds 2 and amount 14 holds 3. Amount 15 tries three coins: best[14] + 1 is 4, best[10] + 1 is 3 and best[3] + 1 is 4. The smallest is 3, so the answer is 3.

Code

def fewest_coins(coins, amount):
    INF = amount + 1                         # more than any real answer
    best = [INF] * (amount + 1)              # best[a] = fewest coins that make a
    best[0] = 0
    for a in range(1, amount + 1):
        for c in coins:
            if c <= a and best[a - c] + 1 < best[a]:
                best[a] = best[a - c] + 1
    return -1 if best[amount] == INF else best[amount]

Time and memory

O(amount x number of coins) time, because each table cell tries each coin once (at most 20,000 x 8 steps). O(amount) memory for the table.

Common mistakes

  • Taking the largest coin first. With coins 1, 5, 12 and amount 15 it uses 4 coins, not 3.
  • Returning the marker value instead of -1 when the amount cannot be made.
  • Reading the table at a negative position when a coin is larger than the amount. Check that the coin fits first.
  • Recursing with no memory table. The same amounts are solved again and again.

Related problems

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