Fewest Coins

Write the code, see it run, pass the checks.

The problem

  • hard
  • Code
  • Pro

Write fewestCoins(coins, amount): the fewest coins that add up to amount, using as many of each value in coins as you like, or -1 if it can't be done (0 coins make 0). Taking the biggest coin first doesn't always work: with coins [1, 3, 4], 6 is 3 + 3 (2 coins), not 4 + 1 + 1. Work out the answer for every amount up from 0 (dynamic programming): the checks go up to 10,000.

Your code must pass

  • Finds the fewest coins
  • Beats taking the biggest coin first
  • Returns -1 when the amount can't be made
  • Returns 0 for an amount of 0
  • Is fast enough for an amount of 10,000