Data Structures & AlgorithmsVery hardCodeforces Div2 D · DP with sorting

Problem: Weighted interval scheduling (each job has a start, finish and profit; choose a set of non-overlapping jobs to maximise profit) is solved with n up to 2 * 10^5. Which algorithm is standard?

  1. A. Sort by profit and pick greedily, O(n log n) and always optimal
  2. B. Try every subset, O(2^n)
  3. C. Plain DP over time units, O(T) with T up to 10^9 and no compression
  4. D. Sort by finish time, then DP where dp[i] = max(dp[i-1], profit[i] + dp[last compatible job]) with binary search for the last compatible job, O(n log n)

Correct answer: D. Sort by finish time, then DP where dp[i] = max(dp[i-1], profit[i] + dp[last compatible job]) with binary search for the last compatible job, O(n log n)

Jobs sorted by finish time let the DP choose each job or skip it, and binary search finds the latest job that ends before the current start. Greedy by profit fails when a big job blocks two smaller ones.

More Coding Interview Problems questions

Exams that ask this topic