Most Profit Assigning Work LeetCode solution in Java

1
class Solution {
2
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
3
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (b[1] - a[1]));
4
for (int i = 0; i < profit.length; i++) {
5
pq.add(new int[] {difficulty[i], profit[i]});
6
}
7
Arrays.sort(worker);
8
int p = 0;
9
for (int i = worker.length - 1; i >= 0 && !pq.isEmpty(); i--) {
10
if (worker[i] >= pq.peek()[0]) p = p + pq.peek()[1];
11
else {
12
while (!pq.isEmpty() && worker[i] < pq.peek()[0]) pq.poll();
13
if (!pq.isEmpty()) p = p + pq.peek()[1];
14
}
15
}
16
return p;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0