-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
52 lines (50 loc) · 1.35 KB
/
Solution.java
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
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
private int[] tasks;
private int[] workers;
private int strength;
private int pills;
private int m;
private int n;
public int maxTaskAssign(int[] tasks, int[] workers, int pills, int strength) {
Arrays.sort(tasks);
Arrays.sort(workers);
this.tasks = tasks;
this.workers = workers;
this.strength = strength;
this.pills = pills;
n = tasks.length;
m = workers.length;
int left = 0, right = Math.min(m, n);
while (left < right) {
int mid = (left + right + 1) >> 1;
if (check(mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
private boolean check(int x) {
int i = 0;
Deque<Integer> q = new ArrayDeque<>();
int p = pills;
for (int j = m - x; j < m; ++j) {
while (i < x && tasks[i] <= workers[j] + strength) {
q.offer(tasks[i++]);
}
if (q.isEmpty()) {
return false;
}
if (q.peekFirst() <= workers[j]) {
q.pollFirst();
} else if (p == 0) {
return false;
} else {
--p;
q.pollLast();
}
}
return true;
}
}