Data Structures & AlgorithmsModerateTCS NQT · DP warm-up
Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP.
Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP.
INF = 10 ** 9
d = [0] + [INF] * 63
for v in range(1, 64):
d[v] = min(d[v - c] + 1 for c in (1, 5, 10, 25) if v >= c)
print(d[63])What is printed?- A. 7
- B. 9
- C. 73
- D. 6
Correct answer: D. 6
25 + 25 + 10 + 1 + 1 + 1 = 63 uses six coins, and the DP confirms nothing smaller exists. Building the table one size too far and reading d[64] gives 7, forgetting the 25 coin gives 9 (six dimes and three pennies), and counting the number of ways instead of the minimum gives 73.
More Coding Interview Problems questions
- Problem: Climbing stairs 0 to 5 with steps of 1 or 2, but stair 3 is broken and cannot be stood on. How many ways are there to reach stair…
- Problem: Is 'cdeab' a rotation of 'abcde'? Check with the substring test below. What is printed? python s, t = 'abcde', 'cdeab'…
- Problem: Find the minimum cost of a Hamiltonian tour starting and ending at city 0, with symmetric distances D =…
- Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code. python print(sum(1 for a in range(1, 7) for b in…
- Problem: What is the Pisano period of the Fibonacci sequence modulo 10 (the length after which (F(n), F(n+1)) mod 10 repeats (0, 1))? Run…
- Problem: How many 4-digit numbers (1000 to 9999) are divisible by 11? Run the code. python print(sum(1 for x in range(1000, 10000) if x %…