Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

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

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

Correct answer: D. 5

The non-canonical coin system breaks greedy, so the DP finds the true minimum.

More Coding Interview Problems questions

Exams that ask this topic