The sweep line
How a sweep line finds the peak number of overlapping intervals in O(n log n), why ties at equal timestamps matter, and when you'd switch to a heap.
Peak overlap equals minimum rooms
When a question asks for the fewest meeting rooms (or servers or railway platforms) needed to host a schedule, the answer always turns out to be the largest number of bookings running at the exact same instant. You clearly can't get away with fewer rooms than that peak, and because a room becomes free the moment its booking finishes, walking through the bookings in start order and grabbing any available room will probably never need more rooms than that peak either (in fact it never can).
I'd be very careful not to count overlapping pairs by accident. Suppose you've got four meetings running from 0 to 10 and from 1 to 3 and from 4 to 6 and from 7 to 9. You'd count three overlapping pairs because the long meeting clashes with all three short ones, although you only ever need 2 rooms (Room 1 holds 0 to 10 all day while Room 2 hosts the three short meetings one after another). The same thing happens on a three-meeting chain from 1 to 3 and 2 to 5 and 4 to 6, which has two overlapping pairs and still only needs 2 rooms.
Two-array sweep for minimum rooms
minMeetingRooms(meetings)
- I'd split the meetings into two separate sorted lists holding all start times in one and all end times in the other.
- Keep a room count alongside an index j pointing at the earliest meeting that hasn't given its room back yet.
- Walk through every start time in ascending order.
- If ends[j] is at most the current start then an earlier meeting has already finished so you reuse its room and advance j.
- Otherwise every open room is still occupied so you open another room and add 1 to your count.
Sorting the starts and the ends costs O(n log n) while the walk across both arrays stays linear, and as far as I can tell once you only care about the room count you don't even need to remember which start belonged to which end.
Here's the trace on four staggered meetings (1 to 3 and 2 to 4 and 3 to 5 and 4 to 6), where you'll notice at time 3 and time 4 that a finishing meeting frees its room just in time to keep the total at 2.
-
Step 1 / 5starts10213243ends30415263rooms0
Starts sorted: 1, 2, 3, 4. Ends sorted: 3, 4, 5, 6. No rooms are open yet.
-
Step 2 / 5starts10213243ends30415263rooms1
A meeting starts at 1, before the earliest end (3). Every room is busy: open another. Rooms: 1.
-
Step 3 / 5starts10213243ends30415263rooms2
A meeting starts at 2, before the earliest end (3). Every room is busy: open another. Rooms: 2.
-
Step 4 / 5starts10213243ends30415263rooms2
A meeting starts at 3. The earliest end, 3, is no later than 3, so that room is free: reuse it. Still 2 rooms.
-
Step 5 / 5starts10213243ends30415263rooms2
A meeting starts at 4. The earliest end, 4, is no later than 4, so that room is free: reuse it. Still 2 rooms. Every meeting has a room: 2 in all.
Ties at the same timestamp decide correctness
Almost every sweep-line bug I've seen in mock interviews happens when one interval finishes at the exact moment another one starts, like a meeting from 1 to 5 followed by a meeting from 5 to 8. Before you write your sort key or your pointer comparison I'd always check whether the statement treats intervals as half-open or closed.
With standard meetings a booking from 1 to 5 frees its room at time 5 for a booking starting at 5, so
you'd want to process ends before starts. In a two-array sweep that means testing ends[j] <= s
(not strictly less), and in an event list of start (+1) and end (-1) events it means putting -1 ahead of +1
whenever two timestamps tie. Your running count then moves cleanly from 0 up to 1 and back to 0 and up to 1
and finishes with a peak of 1. If the problem treats endpoints as inclusive instead (say you're looking for a
point covered by the most closed segments) then both 1 to 5 and 5 to 8 contain point 5, so you'd probably sort
+1 ahead of -1 and watch the count reach 2 at time 5.
I always test two back-to-back bookings (1 to 5 and 5 to 8) by hand before I run my code. Half-open meetings ought to return 1 whereas closed segments sharing point 5 ought to return 2.
Three ways to write the sweep
People write sweep lines in three common styles and they all run in O(n log n), so I'd generally pick the simplest one that actually answers what you've been asked and leave the priority queue alone when a plain count is enough.
| Approach | How it works | When I use it |
|---|---|---|
| Sorted starts and ends | Two pointers comparing ends[j] <= s | Fastest to write when you only need the room count |
| Sorted (+1 and -1) events | Sort 2n events by time and delta and keep a running sum | Best when you also want the busiest timestamp or stretches with count >= k |
| Min-heap of end times | Sort meetings by start and pop if top <= s before pushing the new end | Needed when you have to assign a specific room number to each meeting |
On three meetings (0 to 30 and 5 to 10 and 15 to 20) a min-heap holding each end time alongside its room number puts 0 to 30 into Room 1 and sees that 30 is bigger than 5 so it opens Room 2 for 5 to 10. When 15 to 20 arrives it pops Room 2 (since its end time 10 is at most 15) and reuses Room 2 for 15 to 20, which hands you the exact room assignment alongside the peak count of 2.