Problem Statement
There are n rooms numbered 0 … n − 1. Each meeting picks the lowest-numbered free room; if every room is busy, the meeting waits until the earliest-finishing room frees up — keeping its original duration. Return the room that held the most meetings (ties → the lowest number).
Input
n— total number of rooms.meetings— list of[start, end]intervals.
n = 2
meetings = [[0,10],[1,5],[2,7],[3,4]]Output
A single integer — the index of the most-booked room.
0Constraints
1 ≤ n ≤ 1001 ≤ meetings.length ≤ 10⁵meetings[i].length == 20 ≤ startᵢ < endᵢ ≤ 5·10⁵
Intuition
Sort the meetings by start time and simulate with two heaps:
avail— a min-heap of free room numbers.busy— a min-heap of(end_time, room)for currently-occupied rooms.
For each meeting:
- Pop every room from
busywhoseend_time ≤ meeting.start— they’re free now, push toavail. - If
availis non-empty → pick the smallest room and push it ontobusywithend. - Otherwise → pop the earliest-finishing room from
busy, delay the meeting so it starts exactly when that room frees, push back(free_time + duration, room). - Increment a
count[room]tally.
Finally return the index with the highest count (lowest index breaks ties).
Solution
import heapq
def mostBooked(n: int, meetings: list[list[int]]) -> int:
meetings.sort()
count = [0] * n
avail = list(range(n)) # min-heap of available room numbers
busy = [] # min-heap of (end_time, room)
for start, end in meetings:
# Free up rooms that have finished
while busy and busy[0][0] <= start:
_, room = heapq.heappop(busy)
heapq.heappush(avail, room)
if avail:
room = heapq.heappop(avail)
heapq.heappush(busy, (end, room))
else:
free_time, room = heapq.heappop(busy)
heapq.heappush(busy, (free_time + end - start, room))
count[room] += 1
return count.index(max(count))// Simplified — in production, use a proper MinHeap class.
function mostBooked(n: number, meetings: number[][]): number {
meetings.sort((a, b) => a[0] - b[0]);
const count = new Array(n).fill(0);
const endTimes = new Array(n).fill(0); // when each room frees up
for (const [start, end] of meetings) {
let bestRoom = -1, earliest = Infinity;
for (let r = 0; r < n; r++) {
if (endTimes[r] <= start) { bestRoom = r; break; }
if (endTimes[r] < earliest) { earliest = endTimes[r]; bestRoom = r; }
}
const wait = Math.max(0, endTimes[bestRoom] - start);
endTimes[bestRoom] = start + wait + (end - start);
count[bestRoom]++;
}
return count.indexOf(Math.max(...count));
}Complexity
| Metric | Value | Reason |
|---|---|---|
| Time | O(m log n) | m meetings × heap ops on n rooms |
| Space | O(n) | Heaps and the count array scale with rooms |
Edge Case Thinking
| Scenario | Handling |
|---|---|
n = 1 | Every meeting goes to room 0; delays stack. Answer is 0. |
meetings.length ≤ n | Every meeting gets a distinct room; tie broken by the lowest index. |
| All meetings identical | First gets the lowest room, rest queue on the same heap entry repeatedly. |
| Tie on max count | count.index(max(count)) naturally returns the lowest index. |
Long-duration meeting blocking room 0 | Meetings wait and pile delays; the simulation still terminates in m log n. |
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.