826. 安排工作以达到最大收益

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
# 时间超了,因为暴力解法。
# def search(check,w,z):
# idx = len(difficulty)-1
# while idx>=0 and check(idx,w,z)==False:
# idx -= 1
# return idx
# def check(pdx,w,z)->bool:
# if z[pdx][0] <= w:
# return True
# else:
# return False

# n = len(difficulty)
# m = len(worker)
# worker.sort()
# print(worker)
# z = sorted(list(zip(difficulty,profit)),key=lambda x:x[1])
# print(z)
# profit = sorted(profit)
# ans = 0
# while worker:
# w = worker.pop()
# result_idx = search(check,w,z)
# if result_idx!=-1: # -1 说明干不了。
# ans += z[result_idx][1]
# return ans

jobs = list(zip(difficulty,profit))
jobs.sort() # 默认按照 第一项 也就是 难度排序,找位置时,会在上一个的基础上继续找。
ans = best = i = 0
for worker_ in sorted(worker): #在能够作的里面找利益最大. 下个在上个的基础上继续。
while i<len(jobs) and worker_>=jobs[i][0]:
best = max(best,jobs[i][1])
i += 1
print(worker_, best)
ans += best
return ans