Data Structures & AlgorithmsHardTCS NQT · DP round

Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the DP.

Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the DP.
coins = [2, 5, 10, 1]
INF = float('inf')
dp = [0] + [INF] * 27
for v in range(1, 28):
    for c in coins:
        if c <= v:
            dp[v] = min(dp[v], dp[v - c] + 1)
print(dp[27])
What is printed?
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: B. 4

Three coins can reach at most 30 but cannot hit 27 exactly, so four coins (10 + 10 + 5 + 2) is optimal. The DP tries every coin at every amount.

More Coding Interview Problems questions

Exams that ask this topic