-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_826.java
48 lines (43 loc) · 1.75 KB
/
_826.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
package com.fishercoder.solutions.firstthousand;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _826 {
public static class Solution1 {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
List<int[]> jobs = new ArrayList<>();
for (int i = 0; i < difficulty.length; i++) {
jobs.add(new int[] {difficulty[i], profit[i]});
}
// sort by difficulty level
Collections.sort(jobs, (a, b) -> a[0] - b[0]);
// update the profit values: because a later (with more difficult) job must be able to
// handle a prior job, so we take the more profitable one
// this makes this jobs list in non-decreasing order in both dimensions
for (int i = 0; i < jobs.size() - 1; i++) {
jobs.get(i + 1)[1] = Math.max(jobs.get(i)[1], jobs.get(i + 1)[1]);
}
int maxProfit = 0;
for (int ability : worker) {
maxProfit += binarySearch(ability, jobs);
}
return maxProfit;
}
private int binarySearch(int ability, List<int[]> jobs) {
int left = 0;
int right = jobs.size() - 1;
int maxProfit = 0;
// it's important to use <= here so we don't miss a possible element
while (left <= right) {
int mid = left + (right - left) / 2;
if (ability >= jobs.get(mid)[0]) {
maxProfit = Math.max(jobs.get(mid)[1], maxProfit);
left = mid + 1;
} else {
right = mid - 1;
}
}
return maxProfit;
}
}
}