Meeting Rooms II
Medium
Topics
Given meeting intervals (each [start, end]), return the minimum number of conference rooms required.
Example 1
The answer is the largest number of meetings running at the same instant, not the number of overlapping pairs. At time 5 the shaded lanes are all open at once, so 2 room(s) are needed. Sorting the start and end times separately and sweeping them is enough.
Each lane is one interval drawn on a shared axis, so overlaps line up vertically.
Input: grid = [[0,30],[5,10],[15,20]] Output: 2
Example 2
Input: grid = [[7,10],[2,4]] Output: 1
Constraints
- 1 <= intervals.length <= 10^4
- intervals[i].length == 2
Editorial — intuition, approach, complexity and pitfalls
Intuition
The number of rooms needed at any instant is the number of meetings currently in progress. Sweeping the sorted start and end times and tracking concurrent meetings gives the peak — that peak is the answer.
Approach
- Sort all start times and all end times separately.
- Two-pointer sweep: when the next start is before the next end, a new room is needed; else a room frees up.
- Track the maximum concurrent rooms.
Dry Run
intervals [[0,30], [5,10], [15,20]]
The starts and ends are sorted SEPARATELY and the pairing is thrown away, because
the only question is how many meetings overlap at the busiest moment.
starts [0, 5, 15] ends [10, 20, 30]
start 0 is before end 10 a meeting begins in use 1 peak 1
start 5 is before end 10 a meeting begins in use 2 peak 2
end 10 is not after start 15 a meeting ends in use 1
start 15 is before end 20 a meeting begins in use 2 peak 2
answer 2
Decoupling the two arrays is the move worth remembering. Nobody ever asks which
end belongs to which start; sweeping the two sorted lists together is exactly a
timeline of "+1 room" and "-1 room" events, and the peak of the running count is
the answer.
The comparison is strict `<`. A meeting ending exactly when another starts can
hand over the same room, so that tie has to resolve as an END before a START.
With `<=`, [[1,3],[3,5]] reports 2 rooms where 1 suffices.
Note `cur` goes down as well as up, but `rooms` only ever takes a max -- the
answer is the high-water mark, not the final value.Complexity
- Time — O(n log n): the two sorts dominate.
- Space — O(n): the start/end arrays.
Common Pitfalls
- Use strict
<so a meeting ending exactly when another starts reuses the room.
Full Solution — all languages
class Solution:
def minMeetingRooms(self, grid):
starts=sorted(i[0] for i in grid)
ends=sorted(i[1] for i in grid)
rooms=cur=0; s=e=0
while s<len(starts):
if starts[s]<ends[e]:
cur+=1; s+=1; rooms=max(rooms,cur)
else:
cur-=1; e+=1
return roomsimport java.util.*;
class Solution {
public int minMeetingRooms(int[][] grid) {
int n=grid.length;
int[] starts=new int[n],ends=new int[n];
for(int i=0;i<n;i++) {
starts[i]=grid[i][0];
ends[i]=grid[i][1];
}
Arrays.sort(starts);
Arrays.sort(ends);
int rooms=0,cur=0,s=0,e=0;
while(s<n) {
if(starts[s]<ends[e]) {
cur++;
s++;
rooms=Math.max(rooms,cur);
} else {
cur--;
e++;
}
}
return rooms;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minMeetingRooms(vector<vector<int>>& grid) {
vector<int> starts,ends;
for(auto& iv:grid) {
starts.push_back(iv[0]);
ends.push_back(iv[1]);
}
sort(starts.begin(),starts.end());
sort(ends.begin(),ends.end());
int rooms=0,cur=0,s=0,e=0,n=starts.size();
while(s<n) {
if(starts[s]<ends[e]) {
cur++;
s++;
rooms=max(rooms,cur);
} else {
cur--;
e++;
}
}
return rooms;
}
};/**
* @param {number[][]} grid
* @return {number}
*/ var minMeetingRooms = function(grid) {
const starts=grid.map(x=>x[0]).sort((a,b)=>a-b);
const ends=grid.map(x=>x[1]).sort((a,b)=>a-b);
let rooms=0,cur=0,s=0,e=0;
while(s<starts.length) {
if(starts[s]<ends[e]) {
cur++;
s++;
rooms=Math.max(rooms,cur);
} else {
cur--;
e++;
}
}
return rooms;
};