Meeting Rooms III (LeetCode 2402): Solution and Explanation

Hard
Array
Sorting
Heap

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.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

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

  1. Sort meetings by start time and create count, an array of n zeros.
  2. Put every room index into the unused min-heap and start used as an empty heap of (end_time, room) pairs.
  3. For each meeting (s, e), pop rooms from used while their end time is at most s, and push them into unused.
  4. If unused is empty, pop the earliest-ending room from used and push it back with end time end + (e - s), delaying the meeting.
  5. Otherwise pop the smallest room from unused and push (e, room) into used.
  6. Increment count[room], and after all meetings return count.index(max(count)).

Step-by-step example

n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
  1. [0,10]: no busy rooms, so room 0 is taken. used = [(10,0)], count = [1,0].
  2. [1,5]: room 0 is busy until 10, so take free room 1. used = [(5,1),(10,0)], count = [1,1].
  3. [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].
  4. [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].
  5. 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))

Time and space complexity

Time

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).

Space

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 unused so 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.