12. Meeting Rooms III
The problem can be found at the following link: 🔗 Question Link
🧩 Problem Description
You are given an integer n representing the number of meeting rooms numbered from 0 to n - 1. You are also given a 2D integer array meetings where meetings[i] = [start_i, end_i] indicates that a meeting is scheduled during the half-open time interval [start_i, end_i). All start_i values are unique.
Meeting Allocation Rules:
When a meeting starts, assign it to the available room with the smallest number.
If no rooms are free, delay the meeting until the earliest room becomes available. The delayed meeting retains its original duration.
When a room becomes free, assign it to the delayed meeting with the earliest original start time.
Return the room number that hosts the most meetings. If multiple rooms have the same highest number of meetings, return the smallest room number among them.
📘 Examples
Example 1:
Input:
n = 2
meetings = [[0,6],[2,3],[3,7],[4,8],[6,8]]Output:
Explanation:
Time 0: Both rooms free. Meeting [0,6) → room 0.
Time 2: Room 0 busy, room 1 free. Meeting [2,3) → room 1.
Time 3: Room 1 frees. Meeting [3,7) → room 1.
Time 4: Both busy. Meeting [4,8) delayed.
Time 6: Room 0 frees; delayed [4,8) starts in room 0 at [6,10).
Time 6: Meeting [6,8) arrives; both busy → delayed.
Time 7: Room 1 frees; delayed [6,8) starts in room 1 at [7,9). Counts → room 0 has 2, room 1 has 3.
Example 2:
Input:
Output:
Explanation:
Time 0: All free. [0,8) → room 0.
Time 1: Rooms 1–3 free. [1,4) → room 1.
Time 2: Rooms 2–3 free. [2,3) → room 2.
Time 3: Room 2 frees. [3,4) → room 2. Counts → [1,1,2,0].
🔒 Constraints
$
1 ≤ n ≤ 10^4$$
1 ≤ meetings.size() ≤ 10^4$$
meetings[i].size() == 2$$
0 ≤ start_i < end_i ≤ 10^4$
✅ My Approach
Two Min‐Heaps Simulation
We simulate the process using two min‐heaps:
avail: a min‐heap of available room indices.busy: a min‐heap of pairs(free_time, room_index)for rooms currently occupied.
Key idea: At each meeting arrival, first release all rooms whose free_time ≤ start. If there is an available room, assign the smallest index. Otherwise, take the room that frees the earliest, delay the meeting to that free time, and reassign accordingly.
Algorithm Steps:
Sort
meetingsby start time.Initialize
availwith all room indices0…n-1.Initialize empty
busyheap and count arraycnt[n] = {0}.For each meeting
[s, e]in sorted order:While
busyis nonempty andbusy.top().free_time ≤ s, pop and push itsroom_indexintoavail.If
availis not empty:Pop the smallest room
rfromavail.Push
(e, r)intobusy.Increment
cnt[r].
Else:
Pop
(t, r)frombusy(earliest free).Push
(t + (e - s), r)intobusy(delayed end).Increment
cnt[r].
Return the index of the maximum in
cnt(tie → smallest index).
🧮 Time and Auxiliary Space Complexity
Expected Time Complexity: Each meeting does up to two heap‐operations on heaps of size ≤ n ⇒ O(m log n), where m = number of meetings.
Expected Auxiliary Space Complexity: Storing two heaps and the count array ⇒ O(n + m).
🧠 Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Single multiset of (freeTime,room)
multiset of (freeTime,room)Keep one multiset<pair<long long,int>> rooms, initially (0,0),(0,1)…(0,n−1). For each sorted [s,e]:
Extract the first entry
(t,i)fromrooms.If
t ≤ s, schedule ats; else schedule att.Erase
(t,i)and reinsert(newEnd, i)wherenewEnd = max(t, s) + (e–s).cnt[i]++.
Time: O(m log n)
Space: O(n)
📊 4️⃣ Event-Driven Sweep with Waiting Queue
We build an event list of all meeting start-events, sorted by time. We also maintain:
freeRooms(min-heap of indices),waiting(min-heap of delayed meetings by original start),busy(min-heap of(end_time, room)).
Process by advancing to the next event (either a meeting start or a room freeing):
At each time
T, free all(end_time ≤ T)→ push rooms tofreeRooms.Enqueue any meeting whose start =
Tintowaiting.While
freeRooms&waitingboth nonempty:Pop
ifromfreeRoomsand meetingmfromwaiting,Schedule it: push
(T + duration, i)intobusy,cnt[i]++.
Advance
Tto next event time.
Time: O((m + k) log n) where k = number of delayed assignments
Space: O(n + m)
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
Two-Heap Simulation
🟢 O(m log n)
🟢 O(n + m)
Fast, straightforward
Requires two heaps
Single multiset
🟡 O(m log n)
🟢 O(n)
Only one container
Slightly more complex API
Event-Driven + Waiting Queue
🟡 O((m+k) log n)
🔴 O(n + m)
Models delays exactly
More bookkeeping
✅ Best Choice?
Scenario
Recommended Approach
Large m and n (≤10⁴)
🥇 Two-Heap Simulation
Want a single balanced container
🥈 multiset-based
Exact event modeling
🥉 Event-Driven + Waiting Queue
🧑💻 Code (Java)
🐍 Code (Python)
🧠 Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: 📬 Any Questions?. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated