Meeting Rooms II
The problem
Section titled “The problem”You get a list of meetings as [start, end]. Return the smallest number of rooms that
lets every meeting happen. A meeting that ends at time t and another that starts at t
can use the same room.
[[0,30],[5,10],[15,20]] -> 2[[1,5],[5,10]] -> 1In the first example, [0,30] runs all day, and the other two never overlap each other, so they share the second room.
Key insight
Section titled “Key insight”The answer is the largest number of meetings running at the same moment. Checking every meeting against every room is O(n²).
Notice you don’t need to know which meeting ends, only when the earliest one does. So separate the times: sort all starts, and sort all ends independently. Walk through the starts in order. If the next start is before the earliest unused end, every room is still busy, so add a room. Otherwise some meeting has finished: reuse its room by moving to the next end. The ends pointer only moves when a room is reused.
The common alternative is a min-heap of end times. Sort by start, and for each meeting
pop the heap if its smallest end is <= start, then push the new end. The heap size at
the end is the answer. Same O(n log n), but you must have a heap (TypeScript has none).
Show solutionHide solution
def min_meeting_rooms(intervals: list[list[int]]) -> int: starts = sorted(s for s, _ in intervals) ends = sorted(e for _, e in intervals) rooms = 0 e = 0 # earliest end time not yet "freed" for start in starts: if start < ends[e]: rooms += 1 # nobody has finished yet: need a new room else: e += 1 # someone finished by now: reuse their room return roomsexport function minMeetingRooms(intervals: number[][]): number { const starts = intervals.map(([s]) => s).sort((a, b) => a - b); const ends = intervals.map(([, e]) => e).sort((a, b) => a - b); let rooms = 0; let e = 0; // earliest end time not yet "freed" for (const start of starts) { if (start < ends[e]) { rooms++; // nobody has finished yet: need a new room } else { e++; // someone finished by now: reuse their room } } return rooms;}import java.util.Arrays;
class Solution { public int minMeetingRooms(int[][] intervals) { int n = intervals.length; int[] starts = new int[n]; int[] ends = new int[n]; for (int i = 0; i < n; i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; } Arrays.sort(starts); Arrays.sort(ends); int rooms = 0; int e = 0; // earliest end time not yet "freed" for (int start : starts) { if (start < ends[e]) { rooms++; // nobody has finished yet: need a new room } else { e++; // someone finished by now: reuse their room } } return rooms; }}Complexity
Section titled “Complexity”- Time: O(n log n) — two sorts; the sweep itself is O(n).
- Space: O(n) — the two arrays of starts and ends.
Watch out for
Section titled “Watch out for”- Use
<for the busy check.start < ends[e]means the earlier meeting is still running. Whenstart == ends[e]the room is free, so that branch reuses it. - Sort starts and ends separately. This is fine here because only the counts matter, not which start pairs with which end. It would be wrong if you needed assignments.
- Empty input. Return 0. The loop never runs, so
ends[0]is never read.
Variations
Section titled “Variations”Meeting Rooms (LeetCode 252)
Section titled “Meeting Rooms (LeetCode 252)”Can one person attend all meetings? The trick: sort by start and check that each meeting begins at or after the previous one ends. Any overlap means no.
Minimum Number of Arrows to Burst Balloons (LeetCode 452)
Section titled “Minimum Number of Arrows to Burst Balloons (LeetCode 452)”Balloons are horizontal ranges; one arrow pops every balloon it crosses. The trick: sort by end and shoot at the end of the first balloon. Skip every balloon that starts at or before that point, then shoot again at the next uncovered one.
Car Pooling (LeetCode 1094)
Section titled “Car Pooling (LeetCode 1094)”Trips pick up and drop off passengers; is the capacity ever exceeded? The trick: the
sweep line. Add +passengers at each pickup and -passengers at each drop-off, process in
time order, and check the running total never goes above capacity.