Meeting Rooms II
Meeting Rooms II is a medium intervals problem solved with the line sweep pattern.
The best approach, line sweep (two sorted arrays), runs in O(n log n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from min-heap of end times up.
Problem
Given meeting times as [start, end) intervals, return the minimum number of conference rooms needed to hold all of them.
Examples
Example 1
- Input
intervals = [[0, 30], [5, 10], [15, 20]]- Output
2
Example 2
- Input
intervals = [[7, 10], [2, 4]]- Output
1
Constraints
1 <= intervals.length <= 10^4- Meetings are [start, end); a room freed at 10 can host a meeting starting at 10.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 9 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Min-heap of end times | O(n log n) | O(n) |
| Line sweep (two sorted arrays) | O(n log n) | O(n) |
1Min-heap of end times
O(n log n)O(n)Sort by start. Keep a min-heap of the end times of meetings in progress. Before placing a meeting, free the room that ends earliest if it has ended by now. The heap's largest size is the answer.
- Sort by start.
- For each meeting: if heap.peek() <= start, poll. Offer end.
- Return the heap size (it only grows when a new room is needed).
class Solution {
public int minMeetingRooms(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
PriorityQueue<Integer> ends = new PriorityQueue<>();
for (int[] m : intervals) {
if (!ends.isEmpty() && ends.peek() <= m[0]) ends.poll();
ends.offer(m[1]);
}
return ends.size();
}
}class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda a: a[0])
ends = []
for m in intervals:
if ends and ends[0] <= m[0]:
heapq.heappop(ends)
heapq.heappush(ends, m[1])
return len(ends)class Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) { return a[0] < b[0]; });
priority_queue<int, vector<int>, greater<int>> ends;
for (auto& m : intervals) {
if (!ends.empty() && ends.top() <= m[0]) ends.pop();
ends.push(m[1]);
}
return ends.size();
}
};class MinHeap {
constructor(cmp = (a, b) => a - b) { this.a = []; this.cmp = cmp; }
size() { return this.a.length; }
isEmpty() { return this.a.length === 0; }
peek() { return this.a[0]; }
push(x) {
const a = this.a;
a.push(x);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (this.cmp(a[i], a[p]) >= 0) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop() {
const a = this.a, top = a[0], last = a.pop();
if (a.length > 0) {
a[0] = last;
let i = 0;
while (true) {
const l = 2 * i + 1, r = l + 1;
let m = i;
if (l < a.length && this.cmp(a[l], a[m]) < 0) m = l;
if (r < a.length && this.cmp(a[r], a[m]) < 0) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}
function minMeetingRooms(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
const ends = new MinHeap();
for (const m of intervals) {
if (!ends.isEmpty() && ends.peek() <= m[0]) ends.pop();
ends.push(m[1]);
}
return ends.size();
}2Line sweep (two sorted arrays)
O(n log n)O(n)Sort all start times and all end times separately. Walk the starts; each start needs a room unless some meeting has already ended by then, in which case that room is reused. The running count of rooms in use peaks at the answer.
- starts[], ends[] sorted.
- For each start: if start >= ends[e], e++ (reuse); else rooms++.
class Solution {
public int minMeetingRooms(int[][] intervals) {
int n = intervals.length;
int[] starts = new int[n], 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, e = 0;
for (int s : starts) {
if (s >= ends[e]) e++;
else rooms++;
}
return rooms;
}
}class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
n = len(intervals)
starts, ends = [0] * n, [0] * n
for i in range(n):
starts[i] = intervals[i][0]
ends[i] = intervals[i][1]
starts.sort()
ends.sort()
rooms = e = 0
for s in starts:
if s >= ends[e]:
e += 1
else:
rooms += 1
return roomsclass Solution {
public:
int minMeetingRooms(vector<vector<int>>& intervals) {
int n = intervals.size();
vector<int> starts(n), ends(n);
for (int i = 0; i < n; i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; }
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
int rooms = 0, e = 0;
for (int s : starts) {
if (s >= ends[e]) e++;
else rooms++;
}
return rooms;
}
};function minMeetingRooms(intervals) {
const n = intervals.length;
const starts = new Array(n), ends = new Array(n);
for (let i = 0; i < n; i++) { starts[i] = intervals[i][0]; ends[i] = intervals[i][1]; }
starts.sort((a, b) => a - b);
ends.sort((a, b) => a - b);
let rooms = 0, e = 0;
for (const s of starts) {
if (s >= ends[e]) e++;
else rooms++;
}
return rooms;
}Edge cases to test
- Back-to-back meetings
- All meetings at the same time
Hints
Hint 1
The answer is the maximum number of meetings running at the same moment. Sweep through all start and end times in order.
FAQ
What is the best time complexity for Meeting Rooms II?
Line sweep (two sorted arrays) runs in O(n log n) time and O(n) extra space.
Which pattern does Meeting Rooms II use?
It is a intervals problem that uses the line sweep pattern. Other problems with the same pattern: Car Pooling, Employee Free Time, Divide Intervals Into Minimum Number of Groups.
Is there a brute force solution for Meeting Rooms II?
Yes. Min-heap of end times takes O(n log n) time and O(n) space. Sort by start.
Which edge cases should I test for Meeting Rooms II?
Back-to-back meetings; All meetings at the same time.