Skip to content

Commit 861f369

Browse files
committed
add solution of problem 0826.
1 parent e006b53 commit 861f369

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 安排工作以达到最大收益
2+
3+
### 问题描述
4+
5+
有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益。
6+
7+
现在我们有一些工人。worker[i]是第i个工人的能力,即该工人只能完成难度小于等于worker[i]的工作。
8+
9+
每一个工人都最多只能安排一个工作,但是一个工作可以完成多次。
10+
11+
举个例子,如果3个工人都尝试完成一份报酬为1的同样工作,那么总收益为 $3。如果一个工人不能完成任何工作,他的收益为 $0 。
12+
13+
我们能得到的最大收益是多少?
14+
15+
**示例:**
16+
```
17+
输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
18+
输出: 100
19+
解释: 工人被分配的工作难度是 [4,4,6,6] ,分别获得 [20,20,30,30] 的收益。
20+
```
21+
22+
**提示:**
23+
- 1 <= difficulty.length = profit.length <= 10000
24+
- 1 <= worker.length <= 10000
25+
- difficulty[i], profit[i], worker[i] 的范围是 [1, 10^5]
26+
27+
### 解法
28+
先按工作难度将`difficulty``profit`排序,再将`worker`排序。赚的最多的收益是在其能力之内的
29+
收益最大值。能力高的工人不会比能力低的工人赚的少。**注意**,工作难度高不代表收益一定高。
30+
31+
```python
32+
class Solution:
33+
def maxProfitAssignment(self, difficulty, profit, worker):
34+
ans = 0
35+
worker.sort()
36+
ls = [[difficulty[i], profit[i]] for i in range(len(profit))]
37+
ls.sort(key=lambda x: x[0])
38+
39+
loc = 0
40+
flag = ls[0][1]
41+
leng = len(ls)
42+
for i in worker:
43+
while loc < leng:
44+
if i < ls[loc][0] and loc == 0:
45+
break
46+
elif i < ls[loc][0]:
47+
ans += flag
48+
break
49+
else:
50+
if flag < ls[loc][1]:
51+
flag = ls[loc][1]
52+
loc += 1
53+
else:
54+
ans += flag
55+
return ans
56+
57+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
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
10+
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

0 commit comments

Comments
 (0)