LC.P826[安排工作以达到最大收益]

方法一:排序+双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int n = difficulty.length;
int[][] index = new int[n][2];
for (int i = 0; i < n; ++i) {
index[i] = new int[]{difficulty[i], profit[i]};
}
Arrays.sort(index, (a, b) -> a[0] - b[0]);
Arrays.sort(worker);
int ans = 0, j = 0, maxProfit = 0;
for (int w : worker) {
while (j < n && index[j][0] <= w) {
maxProfit = Math.max(maxProfit, index[j++][1]);
}
ans += maxProfit;
}
return ans;
}
}
  • 时间复杂度:$O(nlogn + mlogm)$
  • 空间复杂度:$O(n)$