Leetcode #2402: Meeting Rooms III
In this guide, we solve Leetcode #2402 Meeting Rooms III in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given an integer n. There are n rooms numbered from 0 to n - 1.
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: Array, Hash Table, Sorting, Simulation, Heap (Priority Queue)
Intuition
Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.
By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.
Approach
Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.
This keeps the solution linear while remaining easy to explain in an interview setting.
Steps:
- Initialize a hash map for seen items or counts.
- Iterate through the input, querying/updating the map.
- Return the first valid result or the final computed value.
Example
Input: n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
Output: 0
Explanation:
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
Python Solution
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort(key=lambda x: x[0])
busy = []
idle = list(range(n))
heapify(idle)
cnt = [0] * n
for s, e in meetings:
while busy and busy[0][0] <= s:
heappush(idle, heappop(busy)[1])
i = 0
if idle:
i = heappop(idle)
heappush(busy, (e, i))
else:
time_end, i = heappop(busy)
heappush(busy, (time_end + e - s, i))
cnt[i] += 1
ans = 0
for i in range(n):
if cnt[ans] < cnt[i]:
ans = i
return ans
Complexity
The time complexity is , and the space complexity is , where and are the number of meeting rooms and meetings respectively. The space complexity is , where and are the number of meeting rooms and meetings respectively.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.