classSolution: defmaxProfitAssignment(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_ insorted(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