-
Notifications
You must be signed in to change notification settings - Fork 3
/
2402.cpp
61 lines (53 loc) · 1.98 KB
/
2402.cpp
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
52
53
54
55
56
57
58
59
60
61
typedef pair<long long, int> P;
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& meetings) {
sort(meetings.begin(), meetings.end());
vector<int> count(n, 0);
priority_queue<P, vector<P>, greater<P>> pq;
set<int> availableRoom;
for (int i = 0; i < n; ++i) availableRoom.insert(i);
long long basetime = 0;
for (auto& meeting : meetings) {
long long duration = meeting[1] - meeting[0];
basetime = max(basetime, (long long)meeting[0]);
while (!pq.empty() && pq.top().first <= basetime) {
auto [t, currRoomIdx] = pq.top();
pq.pop();
availableRoom.insert(currRoomIdx);
}
if (!availableRoom.empty()) {
int roomIdx = *(availableRoom.begin());
availableRoom.erase(roomIdx);
count[roomIdx]++;
pq.push({basetime + duration, roomIdx});
}
else {
auto [t, roomIdx] = pq.top();
pq.pop();
availableRoom.insert(roomIdx);
basetime = t;
// add room with the saming ending time
while (!pq.empty() && pq.top().first <= basetime) {
auto [t, roomIdx] = pq.top();
pq.pop();
availableRoom.insert(roomIdx);
}
// get an empty room
int currRoomIdx = *(availableRoom.begin());
availableRoom.erase(currRoomIdx);
count[roomIdx]++;
pq.push({basetime + duration, currRoomIdx});
}
}
int res = -1;
int maxUse = 0;
for (int i = 0; i < n; ++i) {
if (count[i] > maxUse) {
maxUse = count[i];
res = i;
}
}
return res;
}
};