Meeting Rooms II
Medium · Intervals
On LeetCode, Meeting Rooms II needs LeetCode Premium (checked October 2026). Here it is free: run and submit in Python, JavaScript, Java or C++ with a free account.
Jump to codeFind the minimum rooms needed for meeting intervals with start < end. A room becomes reusable at the meeting’s end time.
Examples
Example 1
intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2
intervals = [[7,10],[2,4]]
Output: 1
Constraints
- 0 <= intervals.length <= 500
- 0 <= intervals[i].start < intervals[i].end <= 1,000,000
Need a hint?
The answer is the largest number of simultaneous active meetings.
Asked in real interviews
Candidates reported versions of Meeting Rooms II from these company interviews. Some company questions are locked and open with Premium or XP.
- Minimum Number of Rooms to Schedule Overlapping MeetingsGoogle
- Compute conflicts and minimum meeting roomsApple
- Compute the Minimum Number of Meeting RoomsWalmart Labs
Solution approach
Sort meetings by start. Maintain a min-heap of active end times. Before a meeting begins, pop all ends ≤ its start. Push its end and record the maximum heap size. Removing every finished meeting keeps the heap equal to actual occupancy.
Complexity
O(n log n) time and O(n) space.
Python reference solution
import heapq
def minMeetingRooms(intervals):
active = []
best = 0
for start, end in sorted(intervals):
while active and active[0] <= start:
heapq.heappop(active)
heapq.heappush(active, end)
best = max(best, len(active))
return best
JavaScript solution
// Binary heap: comesBefore(a, b) decides which item has higher priority.
class Heap {
constructor(comesBefore) { this.items = []; this.comesBefore = comesBefore; }
get size() { return this.items.length; }
peek() { return this.items[0]; }
push(value) {
const a = this.items;
a.push(value);
let i = a.length - 1;
while (i > 0) {
const parent = Math.floor((i - 1) / 2);
if (!this.comesBefore(a[i], a[parent])) break;
[a[i], a[parent]] = [a[parent], a[i]];
i = parent;
}
}
pop() {
const a = this.items, result = a[0], last = a.pop();
if (!a.length) return result;
a[0] = last;
let i = 0;
while (2 * i + 1 < a.length) {
let child = 2 * i + 1;
if (child + 1 < a.length && this.comesBefore(a[child + 1], a[child])) child++;
if (!this.comesBefore(a[child], a[i])) break;
[a[i], a[child]] = [a[child], a[i]];
i = child;
}
return result;
}
}
function minMeetingRooms(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
const active = new Heap((a, b) => a < b);
let best = 0;
for (const [start, end] of intervals) {
while (active.size && active.peek() <= start) active.pop();
active.push(end);
best = Math.max(best, active.size);
}
return best;
}
Java solution
import java.util.*;
class Solution {
public int minMeetingRooms(int[][] intervals) {
Arrays.sort(intervals, Comparator.comparingInt(interval -> interval[0]));
PriorityQueue<Integer> active = new PriorityQueue<>();
int best = 0;
for (int[] interval : intervals) {
while (!active.isEmpty() && active.peek() <= interval[0]) active.remove();
active.add(interval[1]);
best = Math.max(best, active.size());
}
return best;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int minMeetingRooms(vector<vector<int>> &intervals) {
sort(intervals.begin(), intervals.end());
priority_queue<int, vector<int>, greater<int>> active;
int best = 0;
for (auto& interval : intervals) {
while (!active.empty() && active.top() <= interval[0]) active.pop();
active.push(interval[1]);
best = max(best, (int)active.size());
}
return best;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
[
0,
30
],
[
5,
10
],
[
15,
20
]
]
]2
[
[
[
7,
10
],
[
2,
4
]
]
]1
[
[
[
1,
4
],
[
2,
5
],
[
3,
6
]
]
]3
[
[
[
1,
2
],
[
2,
3
],
[
3,
4
]
]
]1
[
[
[
1,
3
],
[
2,
4
],
[
5,
6
]
]
]2
Drafts are saved per language in this browser when storage is available.