LC.826. Most Profit Assigning Work
生活随笔
收集整理的這篇文章主要介紹了
LC.826. Most Profit Assigning Work
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Solution1(object):def maxProfitAssignment(self, difficulty, profit, worker):"""按照利潤排序,然后按照利潤從大到小遍歷,遇到第一個能做的最壞O(n^2)時間復雜度(不是排序,在查找可做的任務時)TLE 51/57"""profit_difficulty = []for i in range(len(profit)):profit_difficulty.append([profit[i],difficulty[i]])profit_difficulty.sort(key=lambda x:x[0], reverse=True)result = 0for i in range(len(worker)):for item in profit_difficulty:if worker[i] >= item[1]:result += item[0]breakreturn resultclass Solution(object):"""將任務按照difficult 從小到大排將worker 從小到大排對于當前的worker遍歷它力所能及的工作并取最大利潤那么下一個worker在上一個worker利潤基礎上,繼續遍歷力所能及的范圍排序后one pass即可這道題的基本邏輯是,如果workerA 比workerB能力大,那么B能獲得的利潤肯定比A高"""def maxProfitAssignment(self, difficulty, profit, worker):jobs = sorted(zip(difficulty, profit))worker.sort()index, cur_max, result = 0, 0, 0for ability in worker:while(index < len(difficulty) and ability >= jobs[index][0]):cur_max = max(cur_max, jobs[index][1])index += 1result += cur_maxreturn result
總結
以上是生活随笔為你收集整理的LC.826. Most Profit Assigning Work的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 傅里叶红外气体分析仪组成
- 下一篇: 芯片生产测试中的CP wafer单片测试