Given an array of meeting time intervals, find the minimum number of conference rooms required to hold all of them.
Example
Input intervals = [[0,30],[5,10],[15,20]]
Output 2
Key Pointers
The number of rooms needed at any instant equals the number of meetings currently in progress — track that count as it rises and falls over time.
Splitting start times and end times into two separately sorted lists turns this into a two-pointer sweep over "events" instead of tracking whole intervals.
A meeting ending exactly when another starts doesn't need a new room, so compare the current start against the earliest unfinished end with a strict <.
Step-by-Step Analysis
Extract all start times into one sorted list and all end times into another.
Walk both lists with two pointers, tracking rooms (currently in use) and max_rooms (the answer so far).
If the current start is before the current earliest end, a new room is needed — increment rooms and advance the start pointer.
Otherwise, a room just freed up — decrement rooms and advance the end pointer.
Update max_rooms after every step and return it once the starts are exhausted.
Complexity
Time: O(n log n) for the sorts — Space: O(n)
Code
def minMeetingRooms(intervals: list[list[int]]) -> int:
starts = sorted(i[0] for i in intervals)
ends = sorted(i[1] for i in intervals)
s = e = 0
rooms = max_rooms = 0
while s < len(starts):
if starts[s] < ends[e]:
rooms += 1
s += 1
else:
rooms -= 1
e += 1
max_rooms = max(max_rooms, rooms)
return max_rooms
Code Explanation
How the solution above works, piece by piece.
Only the count of rooms matters, not which meeting uses which, so start and end times can be sorted separately.
Walk both lists with two pointers, always taking the earlier event.
A start before the next end means a new meeting begins while others are running: rooms += 1.
Otherwise the earliest meeting has ended: a room frees up (rooms -= 1).
The peak of rooms is the answer. O(n log n).
Visual Walkthrough
Running it on intervals = [[0, 30], [5, 10], [15, 20]]:
Sorted start times and sorted end times are walked like a merge. A start before the next end needs an extra room; otherwise a meeting has ended and its room is free. The most rooms in use at once is 2.