Question DSA-GRDY-0005
EasyWhat is the output of this code?
coins, amt, n = [10, 5, 2], 17, 0
for c in coins:
n += amt // c
amt %= c
print(n)
- A.
2
- B.
5
- C.
4
- D.
3
Show answer and explanation
Answer: D
Greedy change takes as many of the largest coin as possible, then the next: 17 = 10 + 5 + 2, so 3 coins. (Greedy is optimal for such canonical coin systems.)