[LeetCode] Meeting Rooms III

2402. Meeting Rooms III

You are given an integer n. There are n rooms numbered from 0 to n - 1.

You are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.

Meetings are allocated to rooms in the following manner:

  1. Each meeting will take place in the unused room with the lowest number.
  2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the same duration as the original meeting.
  3. When a room becomes unused, meetings that have an earlier original start time should be given the room.

Return the number of the room that held the most meetings. If there are multiple rooms, return the room with the lowest number.

A half-closed interval [a, b) is the interval between a and b including a and not including b.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& A) {
long long now = 0;
set<long long> rooms;
unordered_map<long long, long long> count;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> Q;
sort(begin(A), end(A));
for(int i = 0; i < A.size(); i++) {
long long s = A[i][0], e = A[i][1];
while(!Q.empty() and Q.top().first <= s) {
auto [_, id] = Q.top(); Q.pop();
rooms.insert(id);
}
if(now == n) {
if(rooms.empty()) {
auto [time, id] = Q.top(); Q.pop();
Q.push({time + e - s, id});
count[id]++;
} else {
auto r = *rooms.begin();
rooms.erase(r);
Q.push({e, r});
count[r]++;
}
} else {
if(rooms.empty()) {
Q.push({e, now});
count[now]++;
now += 1;
} else {
auto r = *rooms.begin();
rooms.erase(r);
Q.push({e, r});
count[r]++;
}
}
}
long long ma = -1, res = 0;
for(auto [id, freq] : count) {
if(freq == ma) res = min(res, id);
else if(freq > ma) {
ma = freq;
res = id;
}
}

return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/04/PS/LeetCode/meeting-rooms-iii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.