Skip to content

Commit 119a500

Browse files
committed
feat: add solutions to lc problem: No.0826.Most Profit Assigning Work
1 parent 7bd35e2 commit 119a500

File tree

6 files changed

+264
-31
lines changed

6 files changed

+264
-31
lines changed

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

+93-2
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,118 @@
3434
<li><code>difficulty[i], profit[i], worker[i]</code>&nbsp; 的范围是&nbsp;<code>[1, 10^5]</code></li>
3535
</ul>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
4140

41+
“排序 + 双指针”。
42+
4243
<!-- tabs:start -->
4344

4445
### **Python3**
4546

4647
<!-- 这里可写当前语言的特殊实现逻辑 -->
4748

4849
```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
5063
```
5164

5265
### **Java**
5366

5467
<!-- 这里可写当前语言的特殊实现逻辑 -->
5568

5669
```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+
```
57120
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+
}
58149
```
59150

60151
### **...**

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

+91-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,103 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
62+
n = len(difficulty)
63+
job = [(difficulty[i], profit[i]) for i in range(n)]
64+
job.sort(key=lambda x: x[0])
65+
worker.sort()
66+
i = t = res = 0
67+
for w in worker:
68+
while i < n and job[i][0] <= w:
69+
t = max(t, job[i][1])
70+
i += 1
71+
res += t
72+
return res
6173
```
6274

6375
### **Java**
6476

6577
```java
78+
class Solution {
79+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
80+
int n = difficulty.length;
81+
List<int[]> job = new ArrayList<>();
82+
for (int i = 0; i < n; ++i) {
83+
job.add(new int[]{difficulty[i], profit[i]});
84+
}
85+
job.sort(Comparator.comparing(a -> a[0]));
86+
Arrays.sort(worker);
87+
int res = 0;
88+
int i = 0, t = 0;
89+
for (int w : worker) {
90+
while (i < n && job.get(i)[0] <= w) {
91+
t = Math.max(t, job.get(i++)[1]);
92+
}
93+
res += t;
94+
}
95+
return res;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker) {
106+
int n = difficulty.size();
107+
vector<pair<int, int>> job;
108+
for (int i = 0; i < n; ++i)
109+
{
110+
job.push_back({difficulty[i], profit[i]});
111+
}
112+
sort(job.begin(), job.end());
113+
sort(worker.begin(), worker.end());
114+
int i = 0, t = 0;
115+
int res = 0;
116+
for (auto w : worker)
117+
{
118+
while (i < n && job[i].first <= w)
119+
{
120+
t = max(t, job[i++].second);
121+
}
122+
res += t;
123+
}
124+
return res;
125+
}
126+
};
127+
```
66128
129+
### **Go**
130+
131+
```go
132+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) int {
133+
var job [][2]int
134+
for i := range difficulty {
135+
job = append(job, [2]int{difficulty[i], profit[i]})
136+
}
137+
138+
sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] })
139+
sort.Ints(worker)
140+
i, t, n, res := 0, 0, len(difficulty), 0
141+
for _, w := range worker {
142+
for i < n && job[i][0] <= w {
143+
t = max(t, job[i][1])
144+
i++
145+
}
146+
res += t
147+
}
148+
return res
149+
}
150+
151+
func max(a, b int) int {
152+
if a > b {
153+
return a
154+
}
155+
return b
156+
}
67157
```
68158

69159
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker) {
4+
int n = difficulty.size();
5+
vector<pair<int, int>> job;
6+
for (int i = 0; i < n; ++i)
7+
{
8+
job.push_back({difficulty[i], profit[i]});
9+
}
10+
sort(job.begin(), job.end());
11+
sort(worker.begin(), worker.end());
12+
int i = 0, t = 0;
13+
int res = 0;
14+
for (auto w : worker)
15+
{
16+
while (i < n && job[i].first <= w)
17+
{
18+
t = max(t, job[i++].second);
19+
}
20+
res += t;
21+
}
22+
return res;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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] })
8+
sort.Ints(worker)
9+
i, t, n, res := 0, 0, len(difficulty), 0
10+
for _, w := range worker {
11+
for i < n && job[i][0] <= w {
12+
t = max(t, job[i][1])
13+
i++
14+
}
15+
res += t
16+
}
17+
return res
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
3+
int n = difficulty.length;
4+
List<int[]> job = new ArrayList<>();
5+
for (int i = 0; i < n; ++i) {
6+
job.add(new int[]{difficulty[i], profit[i]});
7+
}
8+
job.sort(Comparator.comparing(a -> a[0]));
9+
Arrays.sort(worker);
10+
int res = 0;
11+
int i = 0, t = 0;
12+
for (int w : worker) {
13+
while (i < n && job.get(i)[0] <= w) {
14+
t = Math.max(t, job.get(i++)[1]);
15+
}
16+
res += t;
17+
}
18+
return res;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
class Solution:
2-
def maxProfitAssignment(self, difficulty, profit, worker):
3-
"""
4-
:type difficulty: List[int]
5-
:type profit: List[int]
6-
:type worker: List[int]
7-
:rtype: int
8-
"""
9-
ans = 0
2+
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
3+
n = len(difficulty)
4+
job = [(difficulty[i], profit[i]) for i in range(n)]
5+
job.sort(key=lambda x: x[0])
106
worker.sort()
11-
ls = [[difficulty[i], profit[i]] for i in range(len(profit))]
12-
ls.sort(key=lambda x: x[0])
13-
14-
loc = 0
15-
flag = ls[0][1]
16-
leng = len(ls)
17-
for i in worker:
18-
while loc < leng:
19-
if i < ls[loc][0] and loc == 0:
20-
break
21-
elif i < ls[loc][0]:
22-
ans += flag
23-
break
24-
else:
25-
if flag < ls[loc][1]:
26-
flag = ls[loc][1]
27-
loc += 1
28-
else:
29-
ans += flag
30-
return ans
7+
i = t = res = 0
8+
for w in worker:
9+
while i < n and job[i][0] <= w:
10+
t = max(t, job[i][1])
11+
i += 1
12+
res += t
13+
return res

0 commit comments

Comments
 (0)