-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path27_Meeting_Rooms_III.cpp
More file actions
44 lines (38 loc) · 1.16 KB
/
Copy path27_Meeting_Rooms_III.cpp
File metadata and controls
44 lines (38 loc) · 1.16 KB
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
// 2402. Meeting Rooms III
class Solution
{
public:
int mostBooked(int n, vector<vector<int>> &meetings)
{
sort(meetings.begin(), meetings.end());
vector<int> count(n, 0);
priority_queue<int, vector<int>, greater<>> free;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> busy;
for (int i = 0; i < n; ++i)
free.push(i);
for (auto &meet : meetings)
{
long long start = meet[0], end = meet[1];
while (!busy.empty() && busy.top().first <= start)
{
free.push(busy.top().second);
busy.pop();
}
if (!free.empty())
{
int room = free.top();
free.pop();
busy.emplace(end, room);
count[room]++;
}
else
{
auto [availTime, room] = busy.top();
busy.pop();
busy.emplace(availTime + (end - start), room);
count[room]++;
}
}
return max_element(count.begin(), count.end()) - count.begin();
}
};