Data Structures & AlgorithmsModerateTCS NQT · DP warm-up

Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP.

Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP.
INF = 10 ** 9
d = [0] + [INF] * 63
for v in range(1, 64):
    d[v] = min(d[v - c] + 1 for c in (1, 5, 10, 25) if v >= c)
print(d[63])
What is printed?
  1. A. 7
  2. B. 9
  3. C. 73
  4. D. 6

Correct answer: D. 6

25 + 25 + 10 + 1 + 1 + 1 = 63 uses six coins, and the DP confirms nothing smaller exists. Building the table one size too far and reading d[64] gives 7, forgetting the 25 coin gives 9 (six dimes and three pennies), and counting the number of ways instead of the minimum gives 73.

More Coding Interview Problems questions

Exams that ask this topic