Meeting Rooms II (LeetCode 253): Solution and Explanation
Given a list of meetings with start and end times, return the smallest number of conference rooms needed so that every meeting has a room. That number equals the largest count of meetings running at the same moment.
Watch it run step by step
Try your own input and write the code →Intuition
A brute force picks every distinct time point and counts how many meetings are active there, taking the maximum. That is O(n²) and repeats a lot of counting. A better framing is to notice that the answer is the peak number of overlapping meetings, because at that peak each meeting needs its own room, and at any other time fewer are enough.
To find the peak, imagine sweeping a vertical line from left to right across a timeline. Each start time pushes the active count up by one and each end time pulls it down by one. The answer is the highest the count ever goes. You do not need to know which meeting ends; only that some meeting ended and freed a room.
That observation lets you split the intervals apart. Sort all start times into one list and all end times into another, then merge them with two pointers like the merge step of merge sort. At each step, if the next start comes strictly before the next end, a meeting begins before any room frees up, so the count rises. Otherwise a meeting ends first and the count falls. When a start and an end are equal, processing the end first lets the room be reused, which is why the comparison is strict.
Approach: Two-pointer sweep over sorted starts and ends
- Build
startas the sorted list of start times andendas the sorted list of end times. - Initialize
resandcountto 0, and pointerssandeto 0. - While
sis less than the number of meetings, comparestart[s]withend[e]. - If
start[s] < end[e], a meeting begins before the earliest pending end, so advancesand incrementcount. - Otherwise a meeting has ended, so advance
eand decrementcount. - After each move, update
res = max(res, count), and returnresonce every start is processed.
Step-by-step example
intervals = [[0,30],[5,10],[15,20]]
start = [0, 5, 15]andend = [10, 20, 30].- 0 < 10: a meeting starts.
count = 1,res = 1, s = 1. - 5 < 10: another starts.
count = 2,res = 2, s = 2. - 15 < 10 is false: the meeting ending at 10 frees a room.
count = 1, e = 1. - 15 < 20: a meeting starts.
count = 2,resstays 2, s = 3. The loop ends and returns 2.
Code: Python, Java, C++
1class Solution:
2 def minMeetingRooms(self, intervals: List[List[int]]) -> int:
3 start = sorted([i[0] for i in intervals])
4 end = sorted([i[1] for i in intervals])
5
6 res, count = 0, 0
7 s, e = 0, 0
8
9 while s < len(intervals):
10 if start[s] < end[e]:
11 s += 1
12 count += 1
13 else:
14 e += 1
15 count -= 1
16 res = max(res, count)
17 return res1class Solution {
2 public int minMeetingRooms(int[][] intervals) {
3 int[] start = new int[intervals.length];
4 int[] end = new int[intervals.length];
5 for (int i = 0; i < intervals.length; i++) {
6 start[i] = intervals[i][0];
7 end[i] = intervals[i][1];
8 }
9 Arrays.sort(start);
10 Arrays.sort(end);
11
12 int res = 0, count = 0;
13 int s = 0, e = 0;
14
15 while (s < intervals.length) {
16 if (start[s] < end[e]) {
17 s++;
18 count++;
19 } else {
20 e++;
21 count--;
22 }
23 res = Math.max(res, count);
24 }
25 return res;
26 }
27}1class Solution {
2public:
3 int minMeetingRooms(vector<vector<int>>& intervals) {
4 vector<int> start, end;
5 for (auto& i : intervals) {
6 start.push_back(i[0]);
7 end.push_back(i[1]);
8 }
9 sort(start.begin(), start.end());
10 sort(end.begin(), end.end());
11
12 int res = 0, count = 0;
13 int s = 0, e = 0;
14
15 while (s < intervals.size()) {
16 if (start[s] < end[e]) {
17 s++;
18 count++;
19 } else {
20 e++;
21 count--;
22 }
23 res = max(res, count);
24 }
25 return res;
26 }
27};Time and space complexity
O(n log n)
Sorting the start and end lists costs O(n log n). The sweep moves each pointer at most n times, so it is O(n).
O(n)
The two sorted lists each hold n values.
Other approaches
Min-heap of end times (time O(n log n), space O(n))
Sort meetings by start. Keep a min-heap of end times for rooms in use; for each meeting, pop the top if it ends at or before the new start, then push the new end. The heap size at the end is the room count. It models actual room reuse, which helps when you also need room assignments.
Difference array or event sweep (time O(n log n), space O(n))
Create events (time, +1) for starts and (time, -1) for ends, sort them with ends before starts at equal times, and track the running sum. With bounded times you can use a counting array instead of sorting.
Common mistakes
- Using
<=instead of<when comparing a start with an end, which treats back-to-back meetings as overlapping and overcounts rooms. - Sorting the intervals only by start and then comparing each with its neighbor, which detects overlap but cannot count how many meetings overlap at once.
- Worrying that splitting starts from ends loses information. It does not; the count only depends on how many have started and ended, not which ones.
- Looping until both pointers are exhausted and returning the final
count, which is 0. Track the running maximum instead.
Frequently asked questions
Why can we sort start and end times separately?
The number of rooms in use at any moment is just the number of meetings that have started minus the number that have ended. Which meeting ended does not matter, so the pairing between a start and its end can be dropped.
Heap or two pointers for meeting rooms II?
Both run in O(n log n). The two-pointer sweep is shorter and uses simple arrays, while the min-heap mirrors real room assignment and extends naturally to variants that ask which room each meeting uses.
What is the time complexity of meeting rooms II?
It is O(n log n) for sorting, plus an O(n) sweep. Space is O(n) for the sorted start and end arrays.