Scheduling & Interval Partitioning
Three classic scheduling problems all yield to greedy algorithms — and all three turn on a single design decision: which key to sort by. Interval scheduling sorts by finish time to pack the most compatible jobs; interval partitioning sorts by start time and proves the rooms needed equal the maximum overlap depth; minimizing maximum lateness sorts by deadline and is justified by an adjacent-swap exchange argument.
╌╌╌╌
The previous lesson introduced the greedy method on its canonical example: activity selection, where we pick a maximum-size set of mutually compatible intervals by repeatedly taking the one that finishes earliest. That was a single problem solved by a single sort key. This lesson zooms out to the whole family of interval and scheduling problems that the greedy method solves, and they turn out to be nearly the same algorithm wearing different hats. What separates them is which key you sort by. Sort by finish time and you maximize how many jobs fit; sort by start time and you minimize how many machines you need; sort by deadline and you minimize how late the worst job runs. Choosing the key correctly, and proving that choice optimal, is the entire problem.
Interval scheduling, recapped
Recall the setup. We are given intervals, interval being the half-open , and two intervals are compatible when they do not overlap. We want a maximum-size set of pairwise compatible intervals, the most jobs we can run on one machine without conflict.1
The greedy rule, proved correct last lesson, is earliest finish time first: sort by , take the first interval, discard everything that overlaps it, and recurse on the rest. The selection itself is one linear scan, so after the sort the algorithm runs in .
The picture is one of greedy never falling behind: pick the earliest-finishing interval each round, and greedy's -th choice frees the machine no later than any rival schedule's -th, so it always has at least as much room to come.
The output is a maximum-size set of mutually compatible intervals, computed in . Everything below reuses this skeleton (sort, scan, exchange-argument proof) with a different key and a different objective.
Interval partitioning: minimize the rooms
Now flip the question. Instead of dropping intervals to fit on one machine, we keep all of them and ask for the fewest machines (rooms, colors, frequencies) needed to run them, where two intervals sharing a machine must be compatible. Phrased as graph coloring: color the intervals so that any two overlapping intervals get different colors, using the minimum number of colors.2
The right invariant is depth. Define the depth at a point as the number of intervals containing , and let be the maximum over all points. Depth is a hard lower bound on rooms, and greedy matches it.
The greedy algorithm sorts by start time and keeps a min-heap of machines keyed by the time each becomes free. For each interval in start order, if the machine that frees earliest is already free by this interval's start, reuse it; otherwise open a new machine.
- 1sort by start time ascending
- 2empty min-heap of machines keyed by free time
- 3for each interval in start order do
- 4if nonempty and then
- 5reuse earliest-freed machine
- 6else
- 7new machinenone free: open one
- 8
- 9
- 10returntotal machines opened
Tracing the scan makes the rule concrete. Intervals enter in start order; each reuses the machine that frees earliest if it is already free, otherwise it opens a new one. The third interval arrives while both open machines are still busy, so it forces a third — and that very moment is a point of depth , the witness behind the lower bound.
Because we processed intervals in start order, no interval we open a machine for overlaps a future interval that already passed, so the depth witness is real, not an artifact of order. The sort is and each interval does heap work, for overall.3 This is exactly LeetCode's Meeting Rooms II and Minimum Number of Arrows in disguise: the first asks for directly; the second asks for the complementary count of points that stab all intervals.
Minimizing maximum lateness
The third problem changes the objective from count to timing. We have one machine and jobs; job needs units of processing and has a deadline . We must order the jobs (the machine runs one at a time, no preemption); if job finishes at time its lateness is , and we want to minimize the maximum lateness across all jobs.1
The greedy rule is earliest deadline first (EDF): ignore the processing times entirely, sort the jobs by deadline , and run them back-to-back in that order with no idle gaps. Those two qualifiers, no idle time and deadline order, are exactly what the proof needs.
So minimizing maximum lateness is, again, sort-and-scan: sort by deadline in , run in that order, done. A small worked run makes the objective concrete: with the jobs in deadline order, each finish time falls out of the running total, and the lateness of the worst job is what we report.
The trio, and the one decision
These three — interval scheduling, interval partitioning, and maximum lateness — are the canonical greedy-on-intervals trio. They share a single shape: sort the intervals or jobs by one key, then make one pass. Everything distinctive about each lives in the key:
| Problem | Objective | Sort key | Optimality proof |
|---|---|---|---|
| Scheduling | max compatible jobs | finish time | stays-ahead / exchange |
| Partitioning | min machines | start time | depth lower bound = greedy |
| Max lateness | min worst lateness | deadline | adjacent-swap exchange |
The lesson is that for greedy interval problems the design work is almost entirely choosing the sort key, and the verification work is a short exchange or stays-ahead argument confirming the choice. Get the key right and the rest is a linear scan.
Takeaways
- Interval scheduling maximizes compatible jobs by earliest finish first; a stays-ahead exchange argument proves optimality, in .
- Interval partitioning minimizes machines by earliest start first with a min-heap of free times; the answer equals the maximum depth , since depth forces machines and greedy never opens more.
- Minimizing maximum lateness on one machine uses earliest deadline first; an adjacent-swap exchange argument shows removing inversions never raises .
- All three are the same sort-then-scan skeleton: the entire design decision is the sort key (finish, start, or deadline), and the proof is a short exchange argument.
Footnotes
- CLRS, Ch. 16 — Greedy Algorithms (§16.1): activity selection by earliest finish time and the structure of single-machine scheduling. ↩ ↩2
- Skiena, § — Scheduling: interval partitioning / coloring and the equivalence of minimum machines with maximum overlap depth. ↩
- Erickson, Ch. — Greedy Algorithms: greedy scheduling proofs by exchange arguments and the sort-key-is-the-algorithm framing. ↩
╌╌ END ╌╌