Meeting Rooms III (LeetCode 2402): Solution and Explanation
There are n rooms numbered 0 to n - 1 and a list of meetings with unique start times. Each meeting takes the lowest-numbered free room; if none is free, it waits for the earliest room to free up and keeps its original duration. Return the room that hosted the most meetings, preferring the lowest number on ties.
Watch it run step by step
Try your own input and write the code →Intuition
The rules describe a simulation, so the question is how to run it efficiently. A naive version processes meetings in start order and, for each one, scans all n rooms to find free ones and the one that frees earliest. That is O(m × n), and with up to 100,000 meetings and 100 rooms it is acceptable but wasteful. The real challenge is getting the tie-breaking rules exactly right.
Two questions come up for every meeting, and each maps cleanly to a min-heap. First, which free room has the lowest number? Keep the free room indices in a min-heap called unused. Second, among busy rooms, which frees up soonest, and among those, which has the lowest number? Keep busy rooms in a min-heap called used keyed by (end_time, room), so ties on end time are broken by room number automatically.
Before placing each meeting, release every busy room whose end time is at or before the meeting's start, moving it back into unused. If a free room exists, take the smallest and record when it will end. If not, the meeting must be delayed: pop the busy room that ends first and run the meeting there starting at that end time, so its new end is the old end plus the meeting's duration. Counting how many times each room is assigned gives the answer.
Approach: Simulation with two min-heaps
- Sort
meetingsby start time and createcount, an array of n zeros. - Put every room index into the
unusedmin-heap and startusedas an empty heap of(end_time, room)pairs. - For each meeting
(s, e), pop rooms fromusedwhile their end time is at mosts, and push them intounused. - If
unusedis empty, pop the earliest-ending room fromusedand push it back with end timeend + (e - s), delaying the meeting. - Otherwise pop the smallest room from
unusedand push(e, room)intoused. - Increment
count[room], and after all meetings returncount.index(max(count)).
Step-by-step example
n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
- [0,10]: no busy rooms, so room 0 is taken.
used = [(10,0)],count = [1,0]. - [1,5]: room 0 is busy until 10, so take free room 1.
used = [(5,1),(10,0)],count = [1,1]. - [2,7]: nothing ends by 2 and no room is free. Pop (5,1) and delay the meeting to run 5 to 10 in room 1.
used = [(10,0),(10,1)],count = [1,2]. - [3,4]: nothing ends by 3. Both rooms end at 10, and the heap breaks the tie by room number, so pop (10,0) and run the meeting 10 to 11 in room 0.
count = [2,2]. - Both rooms hosted two meetings, so return the lower number, 0.
Code: Python, Java, C++
1class Solution:
2 def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
3 count = [0] * n
4 meetings.sort()
5
6 unused = [i for i in range(n)] # Min heap
7 used = [] # (end_time, room_index)
8
9 for s, e in meetings:
10 while used and used[0][0] <= s:
11 _, room = heapq.heappop(used)
12 heapq.heappush(unused, room)
13
14 if not unused:
15 end, room = heapq.heappop(used)
16 heapq.heappush(used, (end + (e - s), room))
17 else:
18 room = heapq.heappop(unused)
19 heapq.heappush(used, (e, room))
20 count[room] += 1
21
22 return count.index(max(count))1class Solution {
2 public int mostBooked(int n, int[][] meetings) {
3 int[] count = new int[n];
4 Arrays.sort(meetings, (a, b) -> a[0] - b[0]);
5
6 PriorityQueue<Integer> unused = new PriorityQueue<>();
7 for (int i = 0; i < n; i++) unused.add(i);
8
9 PriorityQueue<long[]> used = new PriorityQueue<>((a, b) -> a[0] != b[0] ? Long.compare(a[0], b[0]) : Long.compare(a[1], b[1]));
10
11 for (int[] m : meetings) {
12 int s = m[0], e = m[1];
13 while (!used.isEmpty() && used.peek()[0] <= s) {
14 unused.add((int)used.poll()[1]);
15 }
16
17 if (unused.isEmpty()) {
18 long[] room = used.poll();
19 long end = room[0], r = room[1];
20 used.add(new long[]{end + (e - s), r});
21 count[(int)r]++;
22 } else {
23 int r = unused.poll();
24 used.add(new long[]{e, r});
25 count[r]++;
26 }
27 }
28
29 int max = 0, res = 0;
30 for (int i = 0; i < n; i++) {
31 if (count[i] > max) {
32 max = count[i];
33 res = i;
34 }
35 }
36 return res;
37 }
38}1class Solution {
2public:
3 int mostBooked(int n, vector<vector<int>>& meetings) {
4 vector<int> count(n, 0);
5 sort(meetings.begin(), meetings.end());
6
7 priority_queue<int, vector<int>, greater<int>> unused;
8 for (int i = 0; i < n; i++) unused.push(i);
9
10 priority_queue<pair<long, int>, vector<pair<long, int>>, greater<pair<long, int>>> used;
11
12 for (auto& m : meetings) {
13 int s = m[0], e = m[1];
14 while (!used.empty() && used.top().first <= s) {
15 unused.push(used.top().second);
16 used.pop();
17 }
18
19 if (unused.empty()) {
20 auto [end, r] = used.top(); used.pop();
21 used.push({end + (e - s), r});
22 count[r]++;
23 } else {
24 int r = unused.top(); unused.pop();
25 used.push({e, r});
26 count[r]++;
27 }
28 }
29 return max_element(count.begin(), count.end()) - count.begin();
30 }
31};Time and space complexity
O(m log m + m log n)
Sorting the m meetings costs O(m log m). Each meeting causes at most one push into used and one pop, and each room release is paid for by an earlier push, so the heap work totals O(m log n).
O(n)
Both heaps together hold each of the n rooms exactly once, and count has n entries. The sort may use extra memory for the meetings list.
Other approaches
Linear scan over rooms (time O(m log m + m × n), space O(n))
Keep an array of each room's end time. For each meeting, scan for the lowest-numbered room that is free by the start time; if none, pick the room with the smallest end time, lowest index on ties. With n at most 100 this is simple and fast enough.
Common mistakes
- Releasing only one finished room per meeting. Several rooms can free up before a start time, and all of them must return to
unusedso the lowest index is chosen. - Using
<instead of<=when releasing rooms. A room whose meeting ends exactly at the new start time is free. - Delaying a meeting to its original end time instead of keeping its duration. The new end is the room's end time plus
e - s. - Storing busy rooms keyed only by end time. Ties must be broken by room number, which the
(end_time, room)tuple does for free. In Java or C++, end times can exceed 32-bit range after many delays, so use 64-bit values.
Frequently asked questions
Why use two heaps for meeting rooms III?
One heap answers which free room has the lowest number, and the other answers which busy room frees up first, breaking ties by number. Both questions come up for every meeting, and heaps answer each in O(log n).
How is a delayed meeting handled?
If no room is free, the meeting waits for the busy room that ends first. It keeps its original duration, so its new end time is that room's end time plus e - s.
Why must meetings be sorted first?
Rooms are assigned in the order meetings start, and delayed meetings keep their relative order. Sorting by start time makes the simulation process them in exactly that order.
Can the end times overflow?
Delays accumulate, so with 100,000 long meetings and a single room the end time can reach tens of billions. Python handles this natively, but Java and C++ should store end times as 64-bit integers.