-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1942.cpp
29 lines (29 loc) · 911 Bytes
/
1942.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
typedef pair<int, int> P;
class Solution {
public:
int smallestChair(vector<vector<int>>& times, int targetFriend) {
priority_queue<int, vector<int>, greater<int>> empty;
priority_queue<P, vector<P>, greater<P>> full;
int n = times.size();
for (int i = 0; i < n; ++i) {
empty.push(i);
times[i].push_back(i);
}
sort(times.begin(), times.end());
for (auto& time : times) {
int start = time[0];
int end = time[1];
int idx = time[2];
while (!full.empty() && full.top().first <= start) {
P p = full.top();
full.pop();
empty.push(p.second);
}
int pos = empty.top();
empty.pop();
full.push({end, pos});
if (targetFriend == idx) return pos;
}
return -1;
}
};