-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeetingRoomsIII2402.cpp
More file actions
35 lines (35 loc) · 1.23 KB
/
MeetingRoomsIII2402.cpp
File metadata and controls
35 lines (35 loc) · 1.23 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
class Solution {
public:
int mostBooked(int n, vector<vector<int>>& meetings) {
sort(meetings.begin(), meetings.end());
priority_queue<pair<long long, long long>, std::vector<pair<long long, long long>>, std::greater<pair<long long, long long>>>pq;
priority_queue<int, vector<int>, greater<int>>available;
vector<int>count(n, 0);
int idx = 0, big = 0, ans = INT_MAX;
for (int i = 0; i < n; i++) available.push(i);
for (auto& it: meetings) {
while (!pq.empty() && pq.top().first <= it[0]) {
available.push(pq.top().second);
pq.pop();
}
if (!available.empty()) {
idx = available.top();
available.pop();
pq.push({it[1], idx});
count[idx]++;
} else {
idx = pq.top().second;
pq.push({pq.top().first + it[1]-it[0], idx});
pq.pop();
count[idx]++;
}
if (count[idx] == big) {
ans = min(idx, ans);
} else if (count[idx] > big) {
big = count[idx];
ans = idx;
}
}
return ans;
}
};