Search lands in PR-5.1 (Pagefind).

How-to Advanced

Chapter 1 Updated

Meeting Rooms III — Most Booked Room

Two heaps: available rooms by index, busy rooms by end time.

  • Full 6m
  • Revision
  • Flow

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.
Example input
n = 2
meetings = [[0,10],[1,5],[2,7],[3,4]]

Output

A single integer — the index of the most-booked room.

Expected output
0

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ meetings.length ≤ 10⁵
  • meetings[i].length == 2
  • 0 ≤ 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:

  1. Pop every room from busy whose end_time ≤ meeting.start — they’re free now, push to avail.
  2. If avail is non-empty → pick the smallest room and push it onto busy with end.
  3. 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).
  4. Increment a count[room] tally.

Finally return the index with the highest count (lowest index breaks ties).

Solution

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

Complexity

MetricValueReason
TimeO(m log n)m meetings × heap ops on n rooms
SpaceO(n)Heaps and the count array scale with rooms

Edge Case Thinking

ScenarioHandling
n = 1Every meeting goes to room 0; delays stack. Answer is 0.
meetings.length ≤ nEvery meeting gets a distinct room; tie broken by the lowest index.
All meetings identicalFirst gets the lowest room, rest queue on the same heap entry repeatedly.
Tie on max countcount.index(max(count)) naturally returns the lowest index.
Long-duration meeting blocking room 0Meetings 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, and PUBLIC_GISCUS_CATEGORY_ID to enable.