CJCoding With Joseph
15free / daySign inCreate account

Fewest Coins With A Table

Write count_min_coins_dp(amount: int, coins: list[int]) -> int that returns the fewest coins that make amount exactly, or -1 when it cannot be made, using a table instead of greed: best[t] holds the fewest coins for the total t. Build best with an impossible value in every entry (amount + 1 works; call it big) and then set best[0] to 0; loop total over range(1, amount + 1) and, for every coin that fits, let best[total] become best[total - coin] + 1 when that is smaller. The answer is best[amount], or -1 if it still holds the impossible value. This fixes the greedy failure: 30 with coins 25 10 1 is 3 coins. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, the amount and then the space-separated coin values; the tests print the count your function returns.

Expected Output:

3

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & Algorithms
📥 Auto-Input:
30\n25 10 1\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.