PythiloMedium · 25 min · Task 15 of 20 · progress kept in this browser

Exact change

Given coin values and an amount, find the fewest coins that add up to exactly that amount. You may use each coin as many times as you like.

fewest_coins([1, 5, 12], 15)  # 3
fewest_coins([5], 3)          # -1

Taking the biggest coin first fails here: 12 + 1 + 1 + 1 uses four coins, while 5 + 5 + 5 uses three.

Your task

Write fewest_coins(coins, amount) returning the smallest number of coins that sum to amount, or -1 if it cannot be done. An amount of 0 takes 0 coins.