Most Profit Assigning Work - Problem

You have n jobs and m workers. You are given three arrays:

  • difficulty[i] and profit[i] are the difficulty and profit of the i-th job
  • worker[j] is the ability of the j-th worker (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: 197
💡 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=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
Most Profit Assigning Work INPUT Jobs (difficulty, profit): d:2 p:10 d:4 p:20 d:6 p:30 d:8 p:40 d:10 p:50 Workers (ability): 4 5 6 7 difficulty = [2,4,6,8,10] profit = [10,20,30,40,50] worker = [4,5,6,7] ALGORITHM STEPS 1 Sort Jobs by Difficulty Pair and sort: (2,10),(4,20)... 2 Precompute Max Profits Track best profit up to each diff Diff: 2 4 6 8 10 Profit: 10 20 30 40 50 MaxP: 10 20 30 40 50 3 Sort Workers workers: [4,5,6,7] 4 Assign Best Jobs Binary search or two pointers W4 --> d:4 --> p:20 W5 --> d:4 --> p:20 W6 --> d:6 --> p:30 W7 --> d:6 --> p:30 FINAL RESULT Worker Assignments: 4 Ability: 4 Best Job: d=4, p=20 +20 5 Ability: 5 Best Job: d=4, p=20 +20 6 Ability: 6 Best Job: d=6, p=30 +30 7 Ability: 7 Best Job: d=6, p=30 +30 20 + 20 + 30 + 30 = 100 Maximum Profit: OK Key Insight: Precompute max profit for each difficulty level using prefix maximum. This allows O(1) lookup of the best achievable profit for any worker ability. Sort both jobs and workers to efficiently match each worker with their optimal job using two pointers. Time: O(n log n + m log m). TutorialsPoint - Most Profit Assigning Work | Precompute Maximum Profits Approach
Asked in
Google 12 Amazon 8 Facebook 6 Microsoft 5
32.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen