Most Profit Assigning Work - Problem
You have n jobs and m workers. You are given three arrays:
difficulty[i]andprofit[i]are the difficulty and profit of thei-thjobworker[j]is the ability of thej-thworker (can only complete jobs with difficulty ≤worker[j])
Rules:
- Every worker can be assigned at most one job
- One job can be completed multiple times by different workers
- If a worker cannot complete any job, their profit is
0
Return the maximum profit we can achieve after assigning workers to jobs optimally.
Input & Output
Example 1 — Basic Assignment
$
Input:
difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
›
Output:
100
💡 Note:
Worker 1 (ability=4): can do jobs 1,2 → best profit=20. Worker 2 (ability=5): can do jobs 1,2 → best profit=20. Worker 3 (ability=6): can do jobs 1,2,3 → best profit=30. Worker 4 (ability=7): can do jobs 1,2,3 → best profit=30. Total: 20+20+30+30=100
Example 2 — Some Workers Get No Job
$
Input:
difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
›
Output:
0
💡 Note:
All workers have abilities (40,25,25) but minimum job difficulty is 47. No worker can complete any job, so total profit is 0.
Example 3 — Multiple Workers Same Job
$
Input:
difficulty = [68,35,52], profit = [90,17,68], worker = [79,45,85]
›
Output:
324
💡 Note:
Worker 1 (ability=79): can do all jobs → best profit=90. Worker 2 (ability=45): can do job 2 only (diff=35) → profit=17. Worker 3 (ability=85): can do all jobs → best profit=90. Total: 90+17+90+127=324. Wait, let me recalculate: Worker 1: jobs with diff≤79 are all three, max profit=90. Worker 2: jobs with diff≤45 is job 2 (diff=35), profit=17. Worker 3: jobs with diff≤85 are all three, max profit=90. Total: 90+17+90=197
Constraints
- 1 ≤ difficulty.length ≤ 104
- 1 ≤ profit.length ≤ 104
- difficulty.length = profit.length
- 1 ≤ worker.length ≤ 104
- 1 ≤ difficulty[i], profit[i], worker[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Jobs with difficulty/profit pairs and workers with abilities
2
Matching
Each worker gets the highest-paying job they can complete
3
Output
Total profit from optimal job assignments
Key Takeaway
🎯 Key Insight: Sort jobs by difficulty and precompute maximum profits to enable efficient binary search per worker
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code