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?- A. 4
- B. 5
- C. 3
- 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
- Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code. python print(sum(1 for x in…
- Problem: What is the length of the longest subarray of [1, 1, 0, 1, 1, 1, 0, 1] containing at most one zero? Run the sliding-window code…
- Problem: How many distinct products a b arise when a and b are each a face value from 1 to 6? Run the code. python print(len({a b for a in…
- Problem: How many subsets of {1, 2, ..., 6} have a product that is a perfect square? Run the code (the empty set has product 1). python…
- Problem: What is the number of distinct values of floor(n / 2) + floor(n / 3) for 1 <= n <= 50? Run the code. python print(len({n // 2 + n…
- Problem: What is the largest sum of a subarray of length at least 2 in [4, -1, 5, -6, 3, 2]? Run the code. python a = [4, -1, 5, -6, 3, 2]…