Data Structures & AlgorithmsHardTCS NQT · DP round
Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the DP.
Problem: Coins of values 2, 5, 10 and 1 are available with unlimited copies. What is the minimum number of coins summing to 27? Run the DP.
coins = [2, 5, 10, 1]
INF = float('inf')
dp = [0] + [INF] * 27
for v in range(1, 28):
for c in coins:
if c <= v:
dp[v] = min(dp[v], dp[v - c] + 1)
print(dp[27])What is printed?- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
Three coins can reach at most 30 but cannot hit 27 exactly, so four coins (10 + 10 + 5 + 2) is optimal. The DP tries every coin at every amount.
More Coding Interview Problems questions
- Problem: How many ways are there to make change for 100 using coins of 1, 5, 10, 25 and 50 (order does not matter)? Run the code. python…
- Problem: Find the longest palindromic substring length of 'forgeeksskeegfor' by expanding around each centre. python s =…
- Problem: What does the following print? It counts distinct values in each window of size 3. python from collections import Counter a = [1…
- Problem: Rotate the array [1, 2, 3, 4, 5, 6, 7] to the right by k = 3 using the three-reversal method. What is printed? python a = [1, 2…
- Problem: How many unordered pairs of distinct elements of [1, 5, 3, 9, 7, 2] have absolute difference exactly 2? Run the code. python a =…
- Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions…