Choosing by earliest end
Why sorting by end time gives the largest clash-free set, the small counterexamples that break every other greedy rule, and the balloon-piercing twin.
Keeping the most is the same as cancelling the fewest
Whenever a problem asks for the largest set of non-overlapping intervals (or the fewest intervals to cancel so none clash), the greedy rule I'd reach for is sorting by end time and keeping every interval that starts at or after the end of the last one you kept. That's pretty much the entire algorithm, and since cancelling the fewest is just the mirror image of keeping the most, you can use the exact same loop for both questions.
You can convince yourself that earliest end is always safe using a quick swap argument. Imagine any best schedule and look at its very first interval, then swap that interval out for whichever interval in the input finishes earliest. Because the replacement ends no later than the one you swapped out, it can't clash with anything that came afterwards in that best schedule, so the schedule stays valid and keeps the exact same size. Once that first slot is locked in you repeat the exact same swap on all the remaining compatible intervals until you've built the full answer.
Greedy selection by earliest end
minRemovals(bookings)
- I'd sort the bookings by their end time.
- Start with lastEnd set to minus infinity and cancelled set to 0.
- Walk through each booking and keep it whenever its start is at least lastEnd (updating lastEnd to its end).
- If it begins before lastEnd then it clashes with the last kept booking so you add 1 to cancelled.
You pay O(n log n) for the sort by end time and then run a single linear pass, and if you're allowed to sort the input in place your extra memory drops down to O(1).
Here's the trace on five bookings (1 to 10 and 2 to 3 and 4 to 5 and 3 to 6 and 6 to 7) sorted by end time, keeping three bookings (2 to 3 and 4 to 5 and 6 to 7) and cancelling 2.
-
Step 1 / 6bookings, sorted by end2–304–513–626–731–104cancelled0
Sort by end time: 2–3, 4–5, 3–6, 6–7, 1–10. Nothing is kept yet.
-
Step 2 / 6bookings, sorted by end2–304–513–626–731–104last kept ends3cancelled0
2–3 starts at or after the beginning, the end of the last kept booking. Keep it.
-
Step 3 / 6bookings, sorted by end2–304–513–626–731–104last kept ends5cancelled0
4–5 starts at or after 3, the end of the last kept booking. Keep it.
-
Step 4 / 6bookings, sorted by end2–304–513–626–731–104last kept ends5cancelled1
3–6 starts before 5, while the last kept booking is still running. Cancel it.
-
Step 5 / 6bookings, sorted by end2–304–513–626–731–104last kept ends7cancelled1
6–7 starts at or after 5, the end of the last kept booking. Keep it.
-
Step 6 / 6bookings, sorted by end2–304–513–626–731–104last kept ends7cancelled2
1–10 starts before 7, while the last kept booking is still running. Cancel it. 2 cancelled, 3 kept.
Why every other greedy rule has a counterexample
When I run mock interviews I usually ask candidates why they sorted by end time and not by start time or duration. Having a small counterexample ready for each rival rule is much clearer than waving your hands, and I find it also stops you from grabbing the wrong sort when you're under pressure.
| Wrong greedy rule | Counterexample | Rule keeps | Earliest end keeps |
|---|---|---|---|
| Earliest start first | 1 to 10 and 2 to 3 and 4 to 5 and 6 to 7 | 1 (1 to 10 blocks the rest) | 3 (2 to 3 and 4 to 5 and 6 to 7) |
| Shortest duration first | 1 to 5 and 4 to 7 and 6 to 10 | 1 (4 to 7 bridges both) | 2 (1 to 5 and 6 to 10) |
| Fewest overlaps first | Eleven ranges centred on 5 to 8 (two overlaps) bridging 3 to 6 and 6 to 9 | 3 (picks 5 to 8 then 0 to 3 and 9 to 12) | 4 (0 to 3 and 3 to 6 and 6 to 9 and 9 to 12) |
That third case tends to surprise students because picking the interval with the fewest overlaps sounds really sensible at first. Suppose you take four consecutive blocks (0 to 3 and 3 to 6 and 6 to 9 and 9 to 12) and pad the ends with an extra copy of 0 to 3 and 9 to 12 plus two copies each of 2 to 5 and 8 to 11 and finally one bridge from 5 to 8. In those eleven intervals the bridge from 5 to 8 is the only interval with just 2 overlaps (every other interval clashes with 3 or 4 neighbours), so fewest-overlaps grabs 5 to 8 first and knocks out both 3 to 6 and 6 to 9, capping you at 3 while earliest end takes all 4.
The piercing dual: minimum arrows to hit every range
Another well-known question gives you horizontal targets and asks for the fewest vertical arrows needed to pierce all of them, where an arrow at x hits every target covering x inclusively. It looks like a different problem at first glance, although as far as I can tell it's the exact same sort by end time. You fire your first arrow right at the end of whichever target finishes earliest, because pushing that arrow as far right as you can without missing the first target catches as many overlapping targets behind it as possible.
On four targets (1 to 6 and 2 to 8 and 7 to 12 and 10 to 16) you'd shoot at x = 6 (hitting both 1 to 6 and
2 to 8), then skip forward to the first untouched target from 7 to 12 and shoot at x = 12 (hitting both 7 to
12 and 10 to 16), which gives you 2 arrows in total. There's just one small code tweak compared with
non-overlapping bookings, because touching targets share their boundary point and only need a fresh arrow when
s > lastArrow (strictly greater, not greater or equal, so I'd always test touching targets before
submitting).
Notice the duality here: the fewest points needed to pierce every closed interval always equals the largest number of pairwise disjoint closed intervals.