-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
40 lines (40 loc) · 1.19 KB
/
Solution.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
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {
sort(tasks.begin(), tasks.end());
sort(workers.begin(), workers.end());
int n = tasks.size(), m = workers.size();
int left = 0, right = min(m, n);
auto check = [&](int x) {
int p = pills;
deque<int> q;
int i = 0;
for (int j = m - x; j < m; ++j) {
while (i < x && tasks[i] <= workers[j] + strength) {
q.push_back(tasks[i++]);
}
if (q.empty()) {
return false;
}
if (q.front() <= workers[j]) {
q.pop_front();
} else if (p == 0) {
return false;
} else {
--p;
q.pop_back();
}
}
return true;
};
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
};