Data Structures & AlgorithmsHardICPC regional · DP vs greedy round
Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest coin not exceeding the remaining amount. How many coins does greedy use, and how many does an optimal solution use?
- A. greedy 3, optimal 2
- B. greedy 2, optimal 2
- C. greedy 3, optimal 3
- D. greedy 4, optimal 2
Correct answer: A. greedy 3, optimal 2
Greedy takes 4, then 1, then 1 (3 coins), while 3 + 3 uses only 2 coins. Greedy is not optimal for arbitrary coin systems.
More Coding Interview Problems questions
- Problem: What does the following print? python def ed(a, b): m, n = len(a), len(b) dp = [[0] (n + 1) for in range(m + 1)] for i in range(m…
- Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?
- Problem: What does the following print? python a = list(range(10)) print(a[::-3], a[2:8:4])
- Problem: Count subarrays whose sum is divisible by k=5 for a = [4, 5, 0, -2, -3, 1]. python from collections import Counter def f(a, k): c…
- Problem: Kadane's algorithm is started with best = 0 and a running sum initialised to 0, then applied to an array where every number is…
- Problem: You binary search the answer x for 'the minimum possible largest sum when an array is split into k contiguous parts'. Which…