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:
1
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:
n = 4
meetings = [[0,8],[1,4],[3,4],[2,3]]
Output:
2
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
meetings
by start time.Initialize
avail
with all room indices0โฆn-1
.Initialize empty
busy
heap and count arraycnt[n] = {0}
.For each meeting
[s, e]
in sorted order:While
busy
is nonempty andbusy.top().free_time โค s
, pop and push itsroom_index
intoavail
.If
avail
is not empty:Pop the smallest room
r
fromavail
.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++)
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& meetings) {
sort(meetings.begin(), meetings.end());
priority_queue<int, vector<int>, greater<>> avail;
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<>> busy;
vector<int> cnt(n);
for(int i=0;i<n;i++) avail.push(i);
for(auto &m:meetings){
long long s=m[0], e=m[1];
while(!busy.empty() && busy.top().first<=s){
avail.push(busy.top().second);
busy.pop();
}
int r = avail.empty() ? (busy.top().second) : avail.top();
if(avail.empty()) {
long long t=busy.top().first;
busy.pop();
busy.push({t+e-s, r});
} else {
avail.pop();
busy.push({e, r});
}
cnt[r]++;
}
return max_element(cnt.begin(), cnt.end()) - cnt.begin();
}
};
๐งโ๐ป Code (Java)
class Solution {
public int mostBooked(int n, int[][] A) {
int[] cnt = new int[n];
PriorityQueue<int[]> occ = new PriorityQueue<>((a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
PriorityQueue<Integer> avail = new PriorityQueue<>();
for (int i = 0; i < n; i++) avail.offer(i);
Arrays.sort(A, (a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
for (int[] m : A) {
int s = m[0], e = m[1];
while (!occ.isEmpty() && occ.peek()[0] <= s) avail.offer(occ.poll()[1]);
if (!avail.isEmpty()) {
int r = avail.poll();
occ.offer(new int[]{e, r});
cnt[r]++;
} else {
int[] t = occ.poll();
occ.offer(new int[]{t[0] + e - s, t[1]});
cnt[t[1]]++;
}
}
int res = 0;
for (int i = 1; i < n; i++) if (cnt[i] > cnt[res]) res = i;
return res;
}
}
๐ Code (Python)
class Solution:
def mostBooked(self, n, A):
A.sort()
free = list(range(n))
heapq.heapify(free)
used = []
cnt = [0] * n
for s, e in A:
while used and used[0][0] <= s:
_, i = heapq.heappop(used)
heapq.heappush(free, i)
if not free:
t, i = heapq.heappop(used)
heapq.heappush(used, (t + e - s, i))
cnt[i] += 1
else:
i = heapq.heappop(free)
heapq.heappush(used, (e, i))
cnt[i] += 1
return cnt.index(max(cnt))
๐ง 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