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?
- A. Sort by profit and pick greedily, O(n log n) and always optimal
- B. Try every subset, O(2^n)
- C. Plain DP over time units, O(T) with T up to 10^9 and no compression
- 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
- Problem: What does the following print? It returns the next greater element of each position in a circular array (search continues from…
- Problem: How many cells of a 3 x 3 grid are at exactly 2 steps (4-directional moves, no obstacles) from the top-left cell? Run the BFS…
- Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1? python from math import gcd print(sum(1 for a in range(1…
- Problem: What does the following print? python print(sum(bin(i).count('1') for i in range(1, 16)))
- Array a = [5, -3, 2, 4, -6, 1, 3, -2]. How many non-empty contiguous subarrays have a sum in the closed range [0, 4]?
- For the string s = "aabbabbaab", find the length of the longest substring with exactly as many letter a as letter b.