Problem
Given meeting time intervals, return the minimum number of conference rooms required so that no two overlapping meetings share a room.
Original: LeetCode 253: Meeting Rooms II.
Examples
Example 1
`intervals = [[0, 30], [5, 10], [15, 20]]``2`Example 2
`intervals = [[7, 10], [2, 4]]``1`Constraints
intervals[i] = [start, end]withstart < end- LeetCode 253 is a premium problem, so the full official bounds are not reproduced here.
Hints
Hint 1
A meeting that starts at the exact moment another ends can reuse that room. The answer is the peak number of concurrent meetings.
Hint 2
This is a sweep line, or a min-heap of end times. Both compute the maximum overlap.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
Brute force. For every meeting, count how many others overlap it, then take the maximum, which is quadratic.
Optimal. Sort starts and ends. A two-pointer scan increments a room count on a start and decrements it on an end, preferring to free a room when start == end. A min-heap of end times is the same idea: if the next start is at or after the earliest end, reuse that room.
Alternative. A TreeMap of +1 at starts and -1 at ends, then a prefix sum, also finds the peak.
Complexity
- Time
- O(n log n)
O(n log n) time for the two sorts, O(n) extra space.
- Space
- O(n)
Code
def min_meeting_rooms(intervals: list[list[int]]) -> int:
starts = sorted(s for s, _ in intervals)
ends = sorted(e for _, e in intervals)
rooms = peak = j = 0
for start in starts:
if start < ends[j]:
rooms += 1
peak = max(peak, rooms)
else:
j += 1
return peak
if __name__ == "__main__":
assert min_meeting_rooms([[0, 30], [5, 10], [15, 20]]) == 2
assert min_meeting_rooms([[7, 10], [2, 4]]) == 1
print("ok")import java.util.Arrays;
public class MeetingRoomsII {
static 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, peak = 0, j = 0;
for (int start : starts) {
if (start < ends[j]) {
rooms++;
if (rooms > peak) peak = rooms;
} else {
j++;
}
}
return peak;
}
public static void main(String[] args) {
if (minMeetingRooms(new int[][] {{0, 30}, {5, 10}, {15, 20}}) != 2) throw new AssertionError();
if (minMeetingRooms(new int[][] {{7, 10}, {2, 4}}) != 1) throw new AssertionError();
System.out.println("ok");
}
}Edge cases
- Official samples: [[0,30],[5,10],[15,20]] needs 2 rooms, and [[7,10],[2,4]] needs 1.
- A start equal to an end reuses a room, because the comparison is start < end.
Follow-ups
- Meeting Rooms I only asks whether one room is enough, which is a single overlap check after sorting by start.
- My Calendar II tracks double bookings with a similar sweep.
Interview script
"The number of rooms is the peak number of meetings in progress. I sort starts and ends and scan them together: a start that happens before the next end opens a new room, otherwise a room is freed first."
Source: LeetCode 253: Meeting Rooms II
Calendars and time windows
Sources (1)
- LeetCode 253: Meeting Rooms IIleetcode.com