Most profit assigning work
January 6, 2023
greedy two-pointersProblem URL: Most profit assigning work
We will zip together difficulty and profit as jobs and sort them by difficulty. Then we will sort workers. For each worker, we will find the maximum profit he can make under his ability. The maximum profit he can make is the maximum profit of the job with the highest difficulty that is less than or equal to his ability.
class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
jobs = sorted(zip(difficulty, profit))
worker.sort()
res = i = best = 0
for ability in worker:
while i < len(jobs) and ability >= jobs[i][0]:
best = max(best, jobs[i][1])
i += 1
res += best
return res
Time complexity: O(nlog(n))
Space complexity: O(n)