826. Most Profit Assigning Work - Detailed Explanation
Problem Statement
You have two integer arrays:
difficulty[i]andprofit[i]describe the difficulty and profit of job i.worker[j]is the ability of worker j (the maximum difficulty they can handle).
Each worker can be assigned at most one job whose difficulty is ≤ their ability, earning the corresponding profit (or 0 if no job is possible). Return the sum of the maximum profit each worker can earn.
Examples
Example 1
Input
difficulty = [2,4,6,8,10]
profit = [10,20,30,40,50]
worker = [4,5,6,7]
Output 100
Explanation
Worker abilities → [4,5,6,7]
Best jobs per worker:
4 → difficulty≤4 gives max profit 20
5 → difficulty≤5 gives max profit 20
6 → difficulty≤6 gives max profit 30
7 → difficulty≤7 gives max profit 30
Sum = 20+20+30+30 = 100
Example 2
Input
difficulty = [85,47,57]
profit = [24,66,99]
worker = [40,25,25]
Output 0
Explanation
No worker can do any of the listed jobs → earns 0 each → sum = 0
Constraints
1 ≤ difficulty.length == profit.length ≤ 10^51 ≤ worker.length ≤ 10^51 ≤ difficulty[i], profit[i], worker[j] ≤ 10^9
Hints
- For a single worker, how would you pick the best job in O(#jobs)?
- Can you preprocess the jobs so that querying the best profit for any ability takes less than O(#jobs)?
- Which data structure or algorithm lets you quickly find “the largest difficulty ≤ ability”?
Approach 1 – Brute Force
Idea
For each worker, scan all jobs and pick the max profit among those with difficulty ≤ ability.
Steps
- Initialize
total = 0. - For each
winworker:- Let
best = 0. - For each job
i: ifdifficulty[i] ≤ w,best = max(best, profit[i]). - Add
besttototal.
- Let
- Return
total.
Code (Python)
Java Code
Complexity Analysis
- Time O(W × J) where W=number of workers, J=number of jobs
- Space O(1) extra
Approach 2 – Sort + Prefix Max + Binary Search
Idea
- Pair each job
(difficulty[i], profit[i])and sort by difficulty. - Build an array
maxProfitUpTo[i]which is the maximum profit among jobs 0…i. - For each worker ability
w, binary‐search the rightmost job withdifficulty ≤ w, then addmaxProfitUpTo[idx](or 0 if none).
Steps
- Create
jobs = [(d,p) for d,p in zip(difficulty,profit)]and sort byd. - Build
maxProfitUpTowheremaxProfitUpTo[0] = jobs[0].profit for i in 1…n-1: maxProfitUpTo[i] = max(maxProfitUpTo[i-1], jobs[i].profit) - For each
winworker:- Binary‐search
jobsfor the largest indexiwithjobs[i].difficulty ≤ w. - If found, add
maxProfitUpTo[i]tototal.
- Binary‐search
- Return
total.
Code (Python)
Java Code
Complexity Analysis
- Sorting jobs: O(J log J)
- Building prefix max: O(J)
- For W workers: each binary‐search O(log J) → O(W log J)
- Total: O((J+W) log J)
Approach 3 – Two‑Pointer One‑Pass (O(J log J + W log W + J + W))
Idea
Sort both jobs and worker. Sweep through workers in increasing ability, advancing a pointer over the jobs to maintain the best profit so far.
Steps
- Sort
jobsby difficulty; sortworkerabilities. - Initialize
best = 0,total = 0,i = 0(job pointer). - For each
win sortedworker:- While
i < Jandjobs[i].difficulty ≤ w:
best = max(best, jobs[i].profit);i++. - Add
besttototal.
- While
- Return
total.
(Track original worker order only if you need per‐worker results; for sum it doesn’t matter.)
Step‑by‑Step Walkthrough
Using difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]:
jobssorted = [(2,10),(4,20),(6,30),(8,40),(10,50)]workersorted = [4,5,6,7]- Start
i=0,best=0,total=0- w=4: advance
iover (2,10) → best=10; (4,20)→best=20; stop at (6,30). → total+=20 - w=5: no new jobs (next difficulty=6>5) → total+=20 (best stays 20)
- w=6: advance (6,30) → best=30; stop. → total+=30
- w=7: no new jobs → total+=30
- w=4: advance
- Sum = 20+20+30+30 = 100
Common Mistakes
- Not building the prefix max over profits before binary search → you might pick a lower‐profit job with higher difficulty.
- Off‑by‑one in binary search index adjustments.
- Forgetting to sort both lists in the two‑pointer approach.
Edge Cases
- No job has difficulty ≤ any worker → all earn 0.
- Identical difficulties with different profits → prefix max handles it.
- Large values up to 10^9 → watch out for integer overflow when summing many profits (use 64‑bit if needed).
Alternative Variations
- Limit each job to be assigned at most once (becomes matching problem).
- Workers have a time cost per job → maximize profit minus cost.
- Each job yields profit proportional to worker ability above difficulty.
Related Problems
-
Maximum Profit in Job Scheduling – weighted interval scheduling for non‑overlapping jobs.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78