Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP.
Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP.
INF = 10 ** 9
d = [0] + [INF] * 99
for v in range(1, 100):
d[v] = min(d[v - c] + 1 for c in (1, 7, 10) if v >= c)
print(d[99])What is printed?- A. 18
- B. 15
- C. 9
- D. 12
Correct answer: D. 12
The DP gives 12, for example 7 × 7 + 5 × 10. Greedy also gets 12 here; the set fails greedy at 14 (7 + 7 vs 10 + 4·1). Ignoring 7 gives 18, ignoring 10 gives 15, and 9 counts only the tens.
More Coding Interview Problems questions
- Problem: How many palindromic numbers below 10^4 are divisible by 11? Run the code. python print(sum(1 for x in range(1, 10 4) if str(x)…
- Problem: How many integer triples (a, b, c) with 1 <= a <= b <= c <= 10 have a b c = 36? Run the code. python print(sum(1 for a in…
- Problem: How many domino tilings does a 3 x 8 board have? Run the recurrence f(n) = 4 f(n-1) - f(n-2) with f(0) = 1, f(1) = 3. python f =…
- Problem: How many Dyck words (balanced bracket sequences) of length 10 exist? Run the DP. python n = 5 C = [1] + [0] n for m in range(1, n…
- Problem: How many permutations of 1..5 have exactly 2 fixed points? Run the code. python from itertools import permutations print(sum(1…
- Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code. python n = 97 c = 0 while n != 1: n…