Fill a table for the fewest coins
Greedy change fails for coins 1, 3 and 4 with amount 6: taking 4 first needs three coins, while 3 + 3 needs two. Dynamic programming tries every last coin for every smaller amount.
coins = [1, 3, 4]
# fewest coins for amounts 0..4
best = [0, 1, 2, 1, 1]Think before coding
best[a] is one plus the smallest best[a - coin] over coins that fit. Fill amounts from zero upward so every smaller answer is ready. Amounts no combination reaches stay infinite and become -1. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write fewest_coins(coins, amount) returning the minimum number of coins summing to amount, with unlimited coins of each value, or -1 if impossible.
The explanation opens here once every check passes.