Problem Description
You are given n meeting rooms numbered from 0 to n-1 and an array of meetings, where each meeting is represented as [start, end) with unique start times. Meetings are assigned as follows:
- Each meeting uses the unused room with the lowest number.
- If no room is available, the meeting is delayed until any room becomes free. The meeting retains its original duration.
- When multiple meetings are waiting, the one with the earlier original start time gets the room as soon as it is available. Return the room number that held the most meetings (if there are ties, return the lowest numbered room).
Key Insights
- Use a min-heap to track currently available rooms by room number.
- Use another min-heap to manage ongoing meetings, keyed by their finish times.
- Process the meetings in order of their start times since they are unique.
- When no room is available at the meeting’s start time, delay the meeting to the earliest finish time available and update its finish time by adding its original duration.
- Count how many meetings are allocated per room and return the one with the maximum count (resolving ties by room number).
Space and Time Complexity
Time Complexity: O(m log n) where m is the number of meetings, since each meeting may require heap operations on a heap of size at most n. Space Complexity: O(m + n) due to storing meetings for sorting and maintaining the heaps (the busy heap is O(n) and the sort uses O(m)).
Solution
We simulate the meeting room allocation with two heaps:
- Heap for available rooms (storing just the room numbers so that the smallest numbered available room is taken first).
- Heap for ongoing meetings (storing pairs of [end_time, room_number]). For each meeting (processed in order by start time):
- Free any rooms that have finished their meetings by comparing the meeting’s start time with the top of the ongoing meetings heap.
- If there is an available room, assign the meeting immediately.
- Otherwise, delay the meeting until the earliest room is free, update the meeting’s start and finish times accordingly.
- Update the count for each room’s usage.
- Finally, select the room with the maximum meeting count (choosing the smallest room number in case of ties).