Most Profit Assigning Work (LeetCode 826): Solution and Explanation

Medium
Array
Two Pointers
Binary Search
Greedy
Sorting

Each worker can do one job whose difficulty is at most their skill, and jobs can be shared. Return the largest total profit the workers can earn.

Intuition

Workers do not compete: a job can be done by any number of them. So each worker independently takes the best-paying job they are able to do, and the answer is the sum of those choices.

The catch is that a harder job may pay less, so the right job is the one with the highest profit among all jobs with difficulty at most the skill, not simply the hardest one.

If workers are processed from weakest to strongest, the set of doable jobs only grows. A single pointer over jobs sorted by difficulty, plus a running maximum of profit, answers every worker in one pass.

Approach: Sort both, sweep with a running best

  1. Pair each difficulty with its profit and sort the pairs by difficulty.
  2. Sort the worker skills.
  3. For each skill in increasing order, advance pointer i while jobs[i].difficulty <= skill, updating best = max(best, jobs[i].profit).
  4. Add best to the total (it is 0 if no job is unlocked yet).
  5. Return the total.

Step-by-step example

difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
  1. Jobs sorted: (2,10) (4,20) (6,30) (8,40) (10,50). Workers sorted: 4, 5, 6, 7.
  2. Skill 4: unlock (2,10) and (4,20). best = 20, total = 20.
  3. Skill 5: no new job. best = 20, total = 40.
  4. Skill 6: unlock (6,30). best = 30, total = 70.
  5. Skill 7: no new job. best = 30, total = 100.
  6. Return 100.

Code: Python, Java, C++

1class Solution:
2    def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
3        jobs = sorted(zip(difficulty, profit))
4        total = 0
5        best = 0
6        i = 0
7        for skill in sorted(worker):
8            while i < len(jobs) and jobs[i][0] <= skill:
9                best = max(best, jobs[i][1])
10                i += 1
11            total += best
12        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n + m log m)

Sorting jobs and workers dominates. The sweep itself moves the job pointer at most n times and visits each worker once.

Space

O(n)

The sorted list of (difficulty, profit) pairs. Sorting the workers can be done in place.

Other approaches

Best-profit table by difficulty (time O(n + m + D), space O(D))

Since values are at most 10^5, build an array where entry d holds the best profit of any job with difficulty at most d (a prefix maximum). Each worker then reads entry worker[j] directly.

Binary search per worker (time O((n + m) log n), space O(n))

Sort the jobs, compute prefix maximum profits, and for each worker binary search the last job with difficulty at most the skill. No need to sort the workers.

Common mistakes

  • Giving each worker the hardest job they can do. A harder job can pay less, so track the maximum profit instead.
  • Resetting best for each worker. Jobs unlocked for a weaker worker are still available to a stronger one.
  • Restarting the job pointer for each worker, which turns the sweep into O(n * m).
  • Sorting difficulty and profit separately, which breaks the pairing between a job's difficulty and its pay.

Frequently asked questions

Why is it fine for several workers to take the same job?

The problem allows a job to be completed any number of times, so workers never block each other. That is what makes the greedy choice per worker optimal.

What does a worker earn if no job is easy enough?

Zero. The running best starts at 0, so such a worker adds nothing.

Can the total overflow a 32-bit integer?

No. At most 10^4 workers each earn at most 10^5, so the total is at most 10^9, which fits in a 32-bit signed integer.

Do ties in difficulty matter?

No. All jobs with the same difficulty unlock together, and the running maximum keeps the best of them.