Weighted intervals
Why greedy breaks once intervals carry profits, and how sorting by end time with a binary search solves weighted job scheduling in O(n log n).
Why greedy rules break once intervals have profits
Once every booking carries its own reward, picking the earliest finish time stops working and I've watched plenty of students lose ten minutes trying to patch a greedy rule that can't really be saved. You can break earliest finish time with four simple bookings, and picking the highest reward first breaks just as easily on our five-booking example below.
| Greedy rule | Bookings (start to end and profit) | Greedy earns | Best earns |
|---|---|---|---|
| Earliest finish first | 1 to 3 (50) and 2 to 4 (10) and 3 to 5 (40) and 3 to 6 (70) | 90 (1 to 3 and 3 to 5) | 120 (1 to 3 and 3 to 6) |
| Highest profit first | 1 to 3 (20) and 2 to 5 (20) and 4 to 6 (70) and 6 to 9 (60) and 3 to 10 (100) | 120 (3 to 10 and 1 to 3) | 150 (1 to 3 and 4 to 6 and 6 to 9) |
You'll still want to sort every booking by its finish time though, because I think that ordering lines up all your subproblems cleanly. Once the schedule sits in ascending finish order, every earlier booking finishing at or before your current booking begins forms one unbroken prefix at the front of the list, which turns an exponential subset search into a straightforward one-dimensional table.
Skip it, or take it with the best compatible prefix
Suppose best[i] holds the largest profit you can collect from the opening i
bookings, indexed by prefix length so best[0] = 0 represents an empty schedule earning nothing.
Whenever you reach the next booking in finish order you've only got two sensible choices to compare, and I'd
usually write them down before coding. You can skip that booking and keep whatever total you already built for
the previous prefix (best[i - 1]), or you can include it and add its reward onto
best[k] where k counts earlier compatible bookings.
Because the finish times are already sorted, finding that compatible prefix count only needs a single
upper-bound binary search (bisect_right in Python or upper_bound in C++). I'd
probably recommend indexing best by prefix length (and not by zero-based booking index), because
it's far less error-prone at the start of the array. Whenever no earlier booking finishes soon enough your
binary search naturally returns zero and reads best[0] = 0 without needing any special-case
branch.
Weighted interval scheduling in O(n log n)
maxProfitJobs(jobs)
- I'd sort the bookings by finish time and copy those finish times into a separate array called ends.
- Start with best[0] = 0 because an empty prefix doesn't earn anything at all.
- For each booking in finish order, binary search the earlier finish times to count how many end at or before this booking starts.
- Set the next table entry to whichever pays more between skipping the booking or adding its reward to that compatible prefix.
- Once the loop finishes you'll find your answer sitting in the final entry best[n].
The initial sort takes O(n log n) and every booking runs a single logarithmic binary search across the earlier finish times, while storing the sorted bookings alongside the table uses linear memory.
Here's the trace on those five bookings sorted by finish time, where the best table climbs from zero through 20 and 90 up to the final maximum of 150.
-
Step 1 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best00
Sort by end time: 1–3: 20, 2–5: 20, 4–6: 70, 6–9: 60, 3–10: 100. best[0] = 0: with no jobs, no profit.
-
Step 2 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best00201take20skip0
Job 1–3 (profit 20). 0 earlier jobs end by 1, worth at most best[0] = 0. Take it: 20 + 0 = 20. Skip it: 0. best[1] = 20.
-
Step 3 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best00201202take20skip20
Job 2–5 (profit 20). 0 earlier jobs end by 2, worth at most best[0] = 0. Take it: 20 + 0 = 20. Skip it: 20. best[2] = 20.
-
Step 4 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best00201202903take90skip20
Job 4–6 (profit 70). 1 earlier job ends by 4, worth at most best[1] = 20. Take it: 70 + 20 = 90. Skip it: 20. best[3] = 90.
-
Step 5 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best002012029031504take150skip90
Job 6–9 (profit 60). 3 earlier jobs end by 6, worth at most best[3] = 90. Take it: 60 + 90 = 150. Skip it: 90. best[4] = 150.
-
Step 6 / 6jobs, sorted by end (start–end: profit)1–3: 2002–5: 2014–6: 7026–9: 6033–10: 1004best0020120290315041505take120skip150
Job 3–10 (profit 100). 1 earlier job ends by 3, worth at most best[1] = 20. Take it: 100 + 20 = 120. Skip it: 150. best[5] = 150. The most profit is 150.
Reconstructing which jobs you actually picked
Interviewers regularly follow up by asking you to return the actual list of chosen bookings alongside the maximum reward. I wouldn't store a full list of bookings inside every table slot, because copying those lists around would usually push both your runtime and your memory up to quadratic complexity. Once the table finishes filling you can walk backwards from the final slot down to the beginning and recover every selected booking in linear time.
At each position you check whether best[i] equals best[i - 1], and whenever they
match you didn't need that booking so you'd probably step back by one position. Whenever best[i]
happens to be strictly larger you definitely included that booking, so you append it to your schedule and jump
directly back to its compatible prefix count (either by repeating the binary search or saving each cutoff
earlier). On our five-booking example you skip 3 to 10 since both ending entries hold 150, then you pick 6 to
9 and jump to prefix three. From there you pick 4 to 6 and jump to prefix one, then finish by picking 1 to 3
and reversing the collected list.
I'd always watch the boundary inside your binary search, because a booking finishing at time t can usually be followed straight away by another booking starting at time t so you'll want the count of finish times <= start (an upper bound) and not strictly smaller.