Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

Meeting priority follows original start times. Two heaps enforce the room rules: available rooms ordered by identifier, and busy rooms ordered by finish time then identifier. When every room is busy, the next meeting waits for the earliest release but keeps its original duration.

Approach

Process meetings by start time. Release every busy room ending at or before that start. If a room is available, use the smallest ID and keep the original finish time. Otherwise pop the earliest-finishing room and schedule the meeting to finish after that release plus its duration. Increment the chosen room's usage count and return it to the busy heap. Finally scan IDs in increasing order, replacing the answer only on a larger count, which implements the smallest-ID tie rule.

class Solution: def mostBooked(self, n: int, meetings: List[List[int]]) -> int: available = list(range(n)) using = [] res = [0] * n for s, e in sorted(meetings): while using and using[0][0] <= s: time, room = heapq.heappop(using) heapq.heappush(available, room) if available: room = heapq.heappop(available) heapq.heappush(using, [e, room]) else: time, room = heapq.heappop(using) heapq.heappush(using, [time + e - s, room]) res[room] += 1 # return res.index(max(res)) mx = 0 selected = 0 for i, x in enumerate(res): if x > mx: mx = x selected = i return selected

Complexity analysis

Let m = meetings.length and r be the room count. The already ordered room array is a valid heap without heap-building operations, so time is O(m log m + m log(r + 1) + r) for meeting sorting, heap updates, and the final room scan. Auxiliary space is O(m + r) including sorting workspace, room heaps, and usage counts.