Skip to content

Commit 9ffe05e

Browse files
authored
feat: add solutions to lc problem: No.0826 (doocs#2826)
No.0826.Most Profit Assigning Work
1 parent cc937dc commit 9ffe05e

File tree

7 files changed

+179
-134
lines changed

7 files changed

+179
-134
lines changed

solution/0800-0899/0826.Most Profit Assigning Work/README.md

+63-45
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:排序 + 双指针
72+
73+
我们可以将工作按照能力升序排列,然后将工作按照难度升序排列。
74+
75+
然后我们遍历工人,对于每个工人,我们找出他能完成的工作中收益最大的那个,然后将这个收益加到答案中。
76+
77+
时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 `profit``worker` 的长度。
7278

7379
<!-- tabs:start -->
7480

@@ -77,38 +83,35 @@ class Solution:
7783
def maxProfitAssignment(
7884
self, difficulty: List[int], profit: List[int], worker: List[int]
7985
) -> int:
80-
n = len(difficulty)
81-
job = [(difficulty[i], profit[i]) for i in range(n)]
82-
job.sort(key=lambda x: x[0])
8386
worker.sort()
84-
i = t = res = 0
87+
jobs = sorted(zip(difficulty, profit))
88+
ans = mx = i = 0
8589
for w in worker:
86-
while i < n and job[i][0] <= w:
87-
t = max(t, job[i][1])
90+
while i < len(jobs) and jobs[i][0] <= w:
91+
mx = max(mx, jobs[i][1])
8892
i += 1
89-
res += t
90-
return res
93+
ans += mx
94+
return ans
9195
```
9296

9397
```java
9498
class Solution {
9599
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
96-
int n = difficulty.length;
97-
List<int[]> job = new ArrayList<>();
100+
Arrays.sort(worker);
101+
int n = profit.length;
102+
int[][] jobs = new int[n][0];
98103
for (int i = 0; i < n; ++i) {
99-
job.add(new int[] {difficulty[i], profit[i]});
104+
jobs[i] = new int[] {difficulty[i], profit[i]};
100105
}
101-
job.sort(Comparator.comparing(a -> a[0]));
102-
Arrays.sort(worker);
103-
int res = 0;
104-
int i = 0, t = 0;
106+
Arrays.sort(jobs, (a, b) -> a[0] - b[0]);
107+
int ans = 0, mx = 0, i = 0;
105108
for (int w : worker) {
106-
while (i < n && job.get(i)[0] <= w) {
107-
t = Math.max(t, job.get(i++)[1]);
109+
while (i < n && jobs[i][0] <= w) {
110+
mx = Math.max(mx, jobs[i++][1]);
108111
}
109-
res += t;
112+
ans += mx;
110113
}
111-
return res;
114+
return ans;
112115
}
113116
}
114117
```
@@ -117,44 +120,59 @@ class Solution {
117120
class Solution {
118121
public:
119122
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
120-
int n = difficulty.size();
121-
vector<pair<int, int>> job;
123+
sort(worker.begin(), worker.end());
124+
int n = profit.size();
125+
vector<pair<int, int>> jobs;
122126
for (int i = 0; i < n; ++i) {
123-
job.push_back({difficulty[i], profit[i]});
127+
jobs.emplace_back(difficulty[i], profit[i]);
124128
}
125-
sort(job.begin(), job.end());
126-
sort(worker.begin(), worker.end());
127-
int i = 0, t = 0;
128-
int res = 0;
129-
for (auto w : worker) {
130-
while (i < n && job[i].first <= w) {
131-
t = max(t, job[i++].second);
129+
sort(jobs.begin(), jobs.end());
130+
int ans = 0, mx = 0, i = 0;
131+
for (int w : worker) {
132+
while (i < n && jobs[i].first <= w) {
133+
mx = max(mx, jobs[i++].second);
132134
}
133-
res += t;
135+
ans += mx;
134136
}
135-
return res;
137+
return ans;
136138
}
137139
};
138140
```
139141
140142
```go
141-
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
142-
var job [][2]int
143-
for i := range difficulty {
144-
job = append(job, [2]int{difficulty[i], profit[i]})
145-
}
146-
147-
sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] })
143+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
148144
sort.Ints(worker)
149-
i, t, n, res := 0, 0, len(difficulty), 0
145+
n := len(profit)
146+
jobs := make([][2]int, n)
147+
for i, p := range profit {
148+
jobs[i] = [2]int{difficulty[i], p}
149+
}
150+
sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] })
151+
mx, i := 0, 0
150152
for _, w := range worker {
151-
for i < n && job[i][0] <= w {
152-
t = max(t, job[i][1])
153-
i++
153+
for ; i < n && jobs[i][0] <= w; i++ {
154+
mx = max(mx, jobs[i][1])
154155
}
155-
res += t
156+
ans += mx
156157
}
157-
return res
158+
return
159+
}
160+
```
161+
162+
```ts
163+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
164+
const n = profit.length;
165+
worker.sort((a, b) => a - b);
166+
const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]);
167+
jobs.sort((a, b) => a[0] - b[0]);
168+
let [ans, mx, i] = [0, 0, 0];
169+
for (const w of worker) {
170+
while (i < n && jobs[i][0] <= w) {
171+
mx = Math.max(mx, jobs[i++][1]);
172+
}
173+
ans += mx;
174+
}
175+
return ans;
158176
}
159177
```
160178

solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md

+63-45
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Sorting + Two Pointers
72+
73+
We can sort the jobs in ascending order of ability, and then sort the jobs in ascending order of difficulty.
74+
75+
Then we traverse the workers. For each worker, we find the job with the maximum profit that he can complete, and then add this profit to the answer.
76+
77+
The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of the arrays `profit` and `worker` respectively.
7278

7379
<!-- tabs:start -->
7480

@@ -77,38 +83,35 @@ class Solution:
7783
def maxProfitAssignment(
7884
self, difficulty: List[int], profit: List[int], worker: List[int]
7985
) -> int:
80-
n = len(difficulty)
81-
job = [(difficulty[i], profit[i]) for i in range(n)]
82-
job.sort(key=lambda x: x[0])
8386
worker.sort()
84-
i = t = res = 0
87+
jobs = sorted(zip(difficulty, profit))
88+
ans = mx = i = 0
8589
for w in worker:
86-
while i < n and job[i][0] <= w:
87-
t = max(t, job[i][1])
90+
while i < len(jobs) and jobs[i][0] <= w:
91+
mx = max(mx, jobs[i][1])
8892
i += 1
89-
res += t
90-
return res
93+
ans += mx
94+
return ans
9195
```
9296

9397
```java
9498
class Solution {
9599
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
96-
int n = difficulty.length;
97-
List<int[]> job = new ArrayList<>();
100+
Arrays.sort(worker);
101+
int n = profit.length;
102+
int[][] jobs = new int[n][0];
98103
for (int i = 0; i < n; ++i) {
99-
job.add(new int[] {difficulty[i], profit[i]});
104+
jobs[i] = new int[] {difficulty[i], profit[i]};
100105
}
101-
job.sort(Comparator.comparing(a -> a[0]));
102-
Arrays.sort(worker);
103-
int res = 0;
104-
int i = 0, t = 0;
106+
Arrays.sort(jobs, (a, b) -> a[0] - b[0]);
107+
int ans = 0, mx = 0, i = 0;
105108
for (int w : worker) {
106-
while (i < n && job.get(i)[0] <= w) {
107-
t = Math.max(t, job.get(i++)[1]);
109+
while (i < n && jobs[i][0] <= w) {
110+
mx = Math.max(mx, jobs[i++][1]);
108111
}
109-
res += t;
112+
ans += mx;
110113
}
111-
return res;
114+
return ans;
112115
}
113116
}
114117
```
@@ -117,44 +120,59 @@ class Solution {
117120
class Solution {
118121
public:
119122
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
120-
int n = difficulty.size();
121-
vector<pair<int, int>> job;
123+
sort(worker.begin(), worker.end());
124+
int n = profit.size();
125+
vector<pair<int, int>> jobs;
122126
for (int i = 0; i < n; ++i) {
123-
job.push_back({difficulty[i], profit[i]});
127+
jobs.emplace_back(difficulty[i], profit[i]);
124128
}
125-
sort(job.begin(), job.end());
126-
sort(worker.begin(), worker.end());
127-
int i = 0, t = 0;
128-
int res = 0;
129-
for (auto w : worker) {
130-
while (i < n && job[i].first <= w) {
131-
t = max(t, job[i++].second);
129+
sort(jobs.begin(), jobs.end());
130+
int ans = 0, mx = 0, i = 0;
131+
for (int w : worker) {
132+
while (i < n && jobs[i].first <= w) {
133+
mx = max(mx, jobs[i++].second);
132134
}
133-
res += t;
135+
ans += mx;
134136
}
135-
return res;
137+
return ans;
136138
}
137139
};
138140
```
139141
140142
```go
141-
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
142-
var job [][2]int
143-
for i := range difficulty {
144-
job = append(job, [2]int{difficulty[i], profit[i]})
145-
}
146-
147-
sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] })
143+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
148144
sort.Ints(worker)
149-
i, t, n, res := 0, 0, len(difficulty), 0
145+
n := len(profit)
146+
jobs := make([][2]int, n)
147+
for i, p := range profit {
148+
jobs[i] = [2]int{difficulty[i], p}
149+
}
150+
sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] })
151+
mx, i := 0, 0
150152
for _, w := range worker {
151-
for i < n && job[i][0] <= w {
152-
t = max(t, job[i][1])
153-
i++
153+
for ; i < n && jobs[i][0] <= w; i++ {
154+
mx = max(mx, jobs[i][1])
154155
}
155-
res += t
156+
ans += mx
156157
}
157-
return res
158+
return
159+
}
160+
```
161+
162+
```ts
163+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
164+
const n = profit.length;
165+
worker.sort((a, b) => a - b);
166+
const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]);
167+
jobs.sort((a, b) => a[0] - b[0]);
168+
let [ans, mx, i] = [0, 0, 0];
169+
for (const w of worker) {
170+
while (i < n && jobs[i][0] <= w) {
171+
mx = Math.max(mx, jobs[i++][1]);
172+
}
173+
ans += mx;
174+
}
175+
return ans;
158176
}
159177
```
160178

Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
22
public:
33
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
4-
int n = difficulty.size();
5-
vector<pair<int, int>> job;
4+
sort(worker.begin(), worker.end());
5+
int n = profit.size();
6+
vector<pair<int, int>> jobs;
67
for (int i = 0; i < n; ++i) {
7-
job.push_back({difficulty[i], profit[i]});
8+
jobs.emplace_back(difficulty[i], profit[i]);
89
}
9-
sort(job.begin(), job.end());
10-
sort(worker.begin(), worker.end());
11-
int i = 0, t = 0;
12-
int res = 0;
13-
for (auto w : worker) {
14-
while (i < n && job[i].first <= w) {
15-
t = max(t, job[i++].second);
10+
sort(jobs.begin(), jobs.end());
11+
int ans = 0, mx = 0, i = 0;
12+
for (int w : worker) {
13+
while (i < n && jobs[i].first <= w) {
14+
mx = max(mx, jobs[i++].second);
1615
}
17-
res += t;
16+
ans += mx;
1817
}
19-
return res;
18+
return ans;
2019
}
2120
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
2-
var job [][2]int
3-
for i := range difficulty {
4-
job = append(job, [2]int{difficulty[i], profit[i]})
5-
}
6-
7-
sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] })
1+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
82
sort.Ints(worker)
9-
i, t, n, res := 0, 0, len(difficulty), 0
3+
n := len(profit)
4+
jobs := make([][2]int, n)
5+
for i, p := range profit {
6+
jobs[i] = [2]int{difficulty[i], p}
7+
}
8+
sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] })
9+
mx, i := 0, 0
1010
for _, w := range worker {
11-
for i < n && job[i][0] <= w {
12-
t = max(t, job[i][1])
13-
i++
11+
for ; i < n && jobs[i][0] <= w; i++ {
12+
mx = max(mx, jobs[i][1])
1413
}
15-
res += t
14+
ans += mx
1615
}
17-
return res
16+
return
1817
}

0 commit comments

Comments
 (0)