Skip to content

Commit 1d428e9

Browse files
authored
Create 2402. Meeting Rooms III 1 (#838)
2 parents 806b640 + 2416e18 commit 1d428e9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

2402. Meeting Rooms III 1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int mostBooked(int n, vector<vector<int>>& meetings) {
4+
vector<int> booked(n, 0);
5+
priority_queue<int, vector<int>, greater<>> idle;
6+
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> busy;
7+
for (int i = 0; i < n; ++i) { idle.push(i); }
8+
ranges::sort(meetings, [](const auto& lhs, const auto& rhs) { return lhs[0] < rhs[0]; });
9+
for (const auto& m : meetings) {
10+
int start = m[0], end = m[1];
11+
while (!busy.empty() && busy.top().first <= start) {
12+
idle.push(busy.top().second);
13+
busy.pop();
14+
}
15+
if (!idle.empty()) {
16+
int room = idle.top();
17+
idle.pop();
18+
busy.push({end, room});
19+
++booked[room];
20+
} else {
21+
auto p = busy.top();
22+
busy.pop();
23+
p.first += end - start;
24+
busy.push(p);
25+
++booked[p.second];
26+
}
27+
}
28+
int res = -1, cur = -1;
29+
for (int i = 0; i < n; ++i) {
30+
if (booked[i] > cur) {
31+
cur = booked[i];
32+
res = i;
33+
}
34+
}
35+
return res;
36+
}
37+
};

0 commit comments

Comments
 (0)