Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP.

Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP.
INF = 10 ** 9
d = [0] + [INF] * 99
for v in range(1, 100):
    d[v] = min(d[v - c] + 1 for c in (1, 7, 10) if v >= c)
print(d[99])
What is printed?
  1. A. 18
  2. B. 15
  3. C. 9
  4. D. 12

Correct answer: D. 12

The DP gives 12, for example 7 × 7 + 5 × 10. Greedy also gets 12 here; the set fails greedy at 14 (7 + 7 vs 10 + 4·1). Ignoring 7 gives 18, ignoring 10 gives 15, and 9 counts only the tens.

More Coding Interview Problems questions

Exams that ask this topic