Full answer
Problem
Given meetings [start, end), return the minimum number of rooms so that every meeting has a room for its whole duration. A meeting ending at time t frees the room for a meeting starting at t.
Example: [[0, 30], [5, 10], [15, 20]] needs 2 rooms; [[7, 10], [2, 4]] needs 1; [[1, 5], [5, 10]] needs 1.
Constraints: up to 10^4 meetings; times up to 10^6.
Brute force
For each time point (or each meeting start), count how many meetings are running and take the maximum: O(n * T) over a time range T, or O(n^2) over starts.
Optimal approach
Heap:
- Sort by start.
- For each meeting: if the heap's smallest end is <= start, replace it with this meeting's end; else push this end.
- Return the heap size.
Sweep: create events (start, +1) and (end, -1), sort (ends before starts at equal times, which tuple order gives because -1 < 1), and track the running sum and its maximum.
Why it works
The answer is at least the maximum number of meetings overlapping at one instant, because each needs its own room. The heap greedy never uses more: it opens a new room only when every room in use is busy at the new meeting's start, which means that many meetings overlap at that instant. Reusing the room that frees earliest is always safe because any free room works equally well for the current meeting.
Complexity
O(n log n) time for sorting and heap operations; O(n) space.
Code
import heapq
import random
def min_rooms(meetings):
"""Minimum rooms so no two overlapping [start, end) meetings share one.
Sort by start, min-heap of end times of rooms in use. O(n log n)."""
ends = []
for s, e in sorted(meetings):
if ends and ends[0] <= s: # earliest-ending room is free again
heapq.heapreplace(ends, e)
else:
heapq.heappush(ends, e)
return len(ends)
def min_rooms_sweep(meetings):
"""Event sweep: +1 at start, -1 at end; ends sort before starts at the same time."""
events = sorted([(s, 1) for s, _ in meetings] + [(e, -1) for _, e in meetings])
cur = best = 0
for _, delta in events:
cur += delta
best = max(best, cur)
return best
def brute(meetings):
"""Max overlap at any integer time point."""
if not meetings:
return 0
lo, hi = min(s for s, _ in meetings), max(e for _, e in meetings)
return max(sum(s <= t < e for s, e in meetings) for t in range(lo, hi + 1))
if __name__ == "__main__":
assert min_rooms([[0, 30], [5, 10], [15, 20]]) == 2
assert min_rooms([[7, 10], [2, 4]]) == 1
assert min_rooms([[1, 5], [5, 10]]) == 1 # back to back: same room
assert min_rooms([]) == 0 and min_rooms_sweep([]) == 0
rng = random.Random(93)
for _ in range(1000):
ms = []
for _ in range(rng.randint(0, 10)):
s = rng.randint(0, 20)
ms.append([s, s + rng.randint(1, 8)])
assert min_rooms(ms) == min_rooms_sweep(ms) == brute(ms)
print("all tests passed")Save as b93_meeting_rooms.py and run python3 b93_meeting_rooms.py. It prints all tests passed (Python 3.13.5).
Edge cases
No meetings (0). Back-to-back meetings (one room, thanks to <= and the event order). Identical meetings (one room each). Very long meetings overlapping everything.
Pattern
Peak overlap of intervals, computed with a min-heap of end times or a sweep line. Related: car pooling (capacity check with the same sweep), minimum platforms at a train station, CPU task scheduling across servers, and difference arrays when times are small integers.
import heapq
import random
def min_rooms(meetings):
"""Minimum rooms so no two overlapping [start, end) meetings share one.
Sort by start, min-heap of end times of rooms in use. O(n log n)."""
ends = []
for s, e in sorted(meetings):
if ends and ends[0] <= s: # earliest-ending room is free again
heapq.heapreplace(ends, e)
else:
heapq.heappush(ends, e)
return len(ends)
def min_rooms_sweep(meetings):
"""Event sweep: +1 at start, -1 at end; ends sort before starts at the same time."""
events = sorted([(s, 1) for s, _ in meetings] + [(e, -1) for _, e in meetings])
cur = best = 0
for _, delta in events:
cur += delta
best = max(best, cur)
return best
def brute(meetings):
"""Max overlap at any integer time point."""
if not meetings:
return 0
lo, hi = min(s for s, _ in meetings), max(e for _, e in meetings)
return max(sum(s <= t < e for s, e in meetings) for t in range(lo, hi + 1))
if __name__ == "__main__":
assert min_rooms([[0, 30], [5, 10], [15, 20]]) == 2
assert min_rooms([[7, 10], [2, 4]]) == 1
assert min_rooms([[1, 5], [5, 10]]) == 1 # back to back: same room
assert min_rooms([]) == 0 and min_rooms_sweep([]) == 0
rng = random.Random(93)
for _ in range(1000):
ms = []
for _ in range(rng.randint(0, 10)):
s = rng.randint(0, 20)
ms.append([s, s + rng.randint(1, 8)])
assert min_rooms(ms) == min_rooms_sweep(ms) == brute(ms)
print("all tests passed")Min-heap of end times and an event sweep, cross-checked against counting overlaps at every integer time.
Complexity
Time: O(n log n). Space: O(n). When times are small integers, a difference array gives O(n + T).
By level
Fresher: Sorts by start and uses a min-heap of end times, tracing a small example. Explains why back-to-back meetings share a room.
Mid: Gives the lower bound argument and the sweep alternative with correct tie order. Assigns room ids and handles the car pooling variant.
Senior: Discusses booking systems with room attributes and preferences, online allocation, and conflict checks with interval trees. Considers time zones and reservation buffers in real scheduling.
Key points
- Rooms needed = maximum simultaneous meetings
- Min-heap of end times
- Reuse a room when its end <= new start
- Sweep events with ends before starts at ties
- O(n log n)
Common mistakes
- Sorting by end time for the heap method
- Using < instead of <= and opening a room for back-to-back meetings
- Wrong tie order in the sweep
- Counting only pairwise overlaps
Follow-ups
- How would you also assign a specific room to each meeting?
- How does car pooling with capacity use the same sweep?
- How would you handle meetings arriving online?
Tips
- State the lower bound argument
- Clarify back-to-back semantics
- Offer both heap and sweep
Evaluation rubric
- Correct room count
- Correct tie handling
- Correctness argument
- Correct complexity