CodePark/ problems / Fewest Coins
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Dynamic programming

Fewest Coins

mediumabout 25 minutes in an interview

coins lists the coin values you can use, and you have an endless supply of each. Pick coins that add up to exactly amount, using as few coins as possible, and return how many you used. If amount can't be reached at all, return -1.

  • amount is between 0 and 3,000. An amount of 0 takes 0 coins.
  • coins holds between 1 and 12 different positive values, in no set order.
  • Grabbing the biggest coin first can give the wrong answer. Example 1 shows this.
Input: coins = [1, 3, 4], amount = 6 Output: 23 + 3 uses 2 coins. If you take 4 first, you get 4 + 1 + 1, which is 3 coins.
Input: coins = [2], amount = 3 Output: -1
Input: coins = [1], amount = 0 Output: 0