Predict the output
What does this print?
coins, amount = [1, 3, 4], 6
greedy, left = 0, amount
for c in sorted(coins, reverse=True):
greedy += left // c
left %= c
print(greedy)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3
Right: greedy takes 4 + 1 + 1, while 3 + 3 needs only two coins.
One counterexample is enough to rule out greedy, which is why the table checks every coin for every amount.
Why not the others?
- B 2. That is the true best, which greedy misses.
- C 6. Greedy uses the largest coin first, not six ones.