Data Structures & AlgorithmsVery hardICPC regional · knapsack variants
Problem: Items have small values (at most 1000 each, n = 100) but very large weights (up to 10^9), and capacity W = 10^9. Which DP is appropriate?
- A. DP over capacity: O(n * W), which is 10^11 and too slow
- B. DP over total value: minimum weight to reach each value, O(n * sum of values)
- C. Greedy by value density, which is optimal for 0/1 knapsack
- D. Bitmask over items, O(2^n) with n = 100
Correct answer: B. DP over total value: minimum weight to reach each value, O(n * sum of values)
Minimum weight for each total value gives O(n * V_total) = 100 * 10^5 = 10^7 operations. Capacity-indexed DP is too large here, and greedy by density is only optimal for fractional knapsack.
More Coding Interview Problems questions
- Problem: For n = 10^5 elements and q = 10^5 queries, a sqrt-decomposition answers each query in O(sqrt n). Roughly how many elementary…
- Problem: How many scalar multiplications does the naive product of a 3 x 4 matrix and a 4 x 2 matrix need?
- Problem: Using the cost of a spanning tree on the complete graph K4 (Cayley's formula n^(n-2)), how many spanning trees does K4 have?
- Problem: How many decimal palindromes lie in [1, 1000]? Run the code. python print(sum(1 for x in range(1, 1001) if str(x) ==…
- Problem: What is the sum of the squares of the odd numbers from 1 to 10? Run the code. python print(sum(x x for x in range(1, 11) if x % 2…
- Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code. python a, b = 1071, 462…