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?- A. 6
- B. 4
- C. 7
- 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
- Problem: How many distinct arrangements of the letters of 'MISSISSIPPI' exist? Run the code. python import math print(math.factorial(11)…
- Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code. python a = [1, 5, 2, 8…
- Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code. python print(sum(1 for x in…
- Problem: Which algorithm computes the number of shortest paths between all pairs in an unweighted graph in O(V E)?
- Problem: What is the time complexity of Kadane's algorithm extended to 2D (maximum-sum submatrix) with an n x n matrix?
- Problem: A hash table uses chaining, and every insertion first scans the key’s chain to make sure the key is not already present. What is…