Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the minimum number of perfect cubes (1, 8, 27, ...) that sum to 100? Run the DP.

Problem: What is the minimum number of perfect cubes (1, 8, 27, ...) that sum to 100? Run the DP.
INF = 10 ** 9
dp = [0] + [INF] * 100
for v in range(1, 101):
    for c in range(1, 6):
        if c ** 3 <= v:
            dp[v] = min(dp[v], dp[v - c ** 3] + 1)
print(dp[100])
What is printed?
  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: A. 4

Coin-change DP over the cube denominations gives the minimum number of cubes summing to 100.

More Coding Interview Problems questions

Exams that ask this topic