Skip to content

Commit 7b6b7fb

Browse files
authored
Merge pull request chipbk10#103 from chipbk10/TwoPointers
Solve Problem 826 - Most Profit Assigning Work
2 parents b701666 + 065dd26 commit 7b6b7fb

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/contest/ReadMe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ReadMe {
1414
// greedy: 1354[!] 1353[x] 1338[v] 1383[!]
1515
// stack: 1190[!] 975[x] 1381[v] 901[v]
1616
// binary search: 1351[v] 1337[v] 1292[!] 1235[!]
17-
// 2 pointers: 1248[*] 1234[v] 986[v] 826
17+
// 2 pointers: 1248[*] 1234[v] 986[v] 826[x]
1818
// sliding window: 1208[v] 1040[!] 995[!] 567
1919
// linked-list: 1290[v] 1019[!] 876[v] 445
2020
// union-find: 1202[v] 924[v] 947[!] 952
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package twopointers;
2+
3+
import java.util.Arrays;
4+
5+
public class Problem826_MostProfitAssigningWork {
6+
7+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
8+
int n = difficulty.length, A[][] = new int[n][2];
9+
for (int i = 0; i < n; i++) A[i] = new int[] {difficulty[i], profit[i]};
10+
Arrays.sort(A, (a, b) -> a[0]-b[0]);
11+
Arrays.sort(worker);
12+
13+
int res = 0, max = 0;
14+
for (int i = 0, j = 0; i < worker.length; i++) {
15+
while (j < n && A[j][0] <= worker[i]) {
16+
max = Math.max(max, A[j++][1]);
17+
}
18+
res += max;
19+
}
20+
return res;
21+
}
22+
}

0 commit comments

Comments
 (0)