|
34 | 34 | <li><code>difficulty[i], profit[i], worker[i]</code> 的范围是 <code>[1, 10^5]</code></li>
|
35 | 35 | </ul>
|
36 | 36 |
|
37 |
| - |
38 | 37 | ## 解法
|
39 | 38 |
|
40 | 39 | <!-- 这里可写通用的实现逻辑 -->
|
41 | 40 |
|
| 41 | +“排序 + 双指针”。 |
| 42 | + |
42 | 43 | <!-- tabs:start -->
|
43 | 44 |
|
44 | 45 | ### **Python3**
|
45 | 46 |
|
46 | 47 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
47 | 48 |
|
48 | 49 | ```python
|
49 |
| - |
| 50 | +class Solution: |
| 51 | + def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: |
| 52 | + n = len(difficulty) |
| 53 | + job = [(difficulty[i], profit[i]) for i in range(n)] |
| 54 | + job.sort(key=lambda x: x[0]) |
| 55 | + worker.sort() |
| 56 | + i = t = res = 0 |
| 57 | + for w in worker: |
| 58 | + while i < n and job[i][0] <= w: |
| 59 | + t = max(t, job[i][1]) |
| 60 | + i += 1 |
| 61 | + res += t |
| 62 | + return res |
50 | 63 | ```
|
51 | 64 |
|
52 | 65 | ### **Java**
|
53 | 66 |
|
54 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 68 |
|
56 | 69 | ```java
|
| 70 | +class Solution { |
| 71 | + public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { |
| 72 | + int n = difficulty.length; |
| 73 | + List<int[]> job = new ArrayList<>(); |
| 74 | + for (int i = 0; i < n; ++i) { |
| 75 | + job.add(new int[]{difficulty[i], profit[i]}); |
| 76 | + } |
| 77 | + job.sort(Comparator.comparing(a -> a[0])); |
| 78 | + Arrays.sort(worker); |
| 79 | + int res = 0; |
| 80 | + int i = 0, t = 0; |
| 81 | + for (int w : worker) { |
| 82 | + while (i < n && job.get(i)[0] <= w) { |
| 83 | + t = Math.max(t, job.get(i++)[1]); |
| 84 | + } |
| 85 | + res += t; |
| 86 | + } |
| 87 | + return res; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### **C++** |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker) { |
| 98 | + int n = difficulty.size(); |
| 99 | + vector<pair<int, int>> job; |
| 100 | + for (int i = 0; i < n; ++i) |
| 101 | + { |
| 102 | + job.push_back({difficulty[i], profit[i]}); |
| 103 | + } |
| 104 | + sort(job.begin(), job.end()); |
| 105 | + sort(worker.begin(), worker.end()); |
| 106 | + int i = 0, t = 0; |
| 107 | + int res = 0; |
| 108 | + for (auto w : worker) |
| 109 | + { |
| 110 | + while (i < n && job[i].first <= w) |
| 111 | + { |
| 112 | + t = max(t, job[i++].second); |
| 113 | + } |
| 114 | + res += t; |
| 115 | + } |
| 116 | + return res; |
| 117 | + } |
| 118 | +}; |
| 119 | +``` |
57 | 120 |
|
| 121 | +### **Go** |
| 122 | +
|
| 123 | +```go |
| 124 | +func maxProfitAssignment(difficulty []int, profit []int, worker []int) int { |
| 125 | + var job [][2]int |
| 126 | + for i := range difficulty { |
| 127 | + job = append(job, [2]int{difficulty[i], profit[i]}) |
| 128 | + } |
| 129 | +
|
| 130 | + sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] }) |
| 131 | + sort.Ints(worker) |
| 132 | + i, t, n, res := 0, 0, len(difficulty), 0 |
| 133 | + for _, w := range worker { |
| 134 | + for i < n && job[i][0] <= w { |
| 135 | + t = max(t, job[i][1]) |
| 136 | + i++ |
| 137 | + } |
| 138 | + res += t |
| 139 | + } |
| 140 | + return res |
| 141 | +} |
| 142 | +
|
| 143 | +func max(a, b int) int { |
| 144 | + if a > b { |
| 145 | + return a |
| 146 | + } |
| 147 | + return b |
| 148 | +} |
58 | 149 | ```
|
59 | 150 |
|
60 | 151 | ### **...**
|
|
0 commit comments