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?

  1. A. DP over capacity: O(n * W), which is 10^11 and too slow
  2. B. DP over total value: minimum weight to reach each value, O(n * sum of values)
  3. C. Greedy by value density, which is optimal for 0/1 knapsack
  4. 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

Exams that ask this topic