-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3362.cpp
More file actions
27 lines (25 loc) · 772 Bytes
/
3362.cpp
File metadata and controls
27 lines (25 loc) · 772 Bytes
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
class Solution {
public:
int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {
sort(queries.begin(), queries.end());
int n = nums.size();
vector<int> diff(n + 1, 0);
int cur = 0;
int qIdx = 0;
priority_queue<int> maxQ;
for (int i = 0; i < n; ++i) {
cur += diff[i];
while (qIdx < queries.size() && queries[qIdx][0] == i) {
maxQ.push(queries[qIdx][1]);
qIdx++;
}
while (nums[i] > cur && !maxQ.empty() && maxQ.top() >= i) {
cur++;
diff[maxQ.top() + 1] -= 1;
maxQ.pop();
}
if (nums[i] > cur) return -1;
}
return maxQ.size();
}
};