Maximize Profit from Task Assignment - Problem

You are given an integer array workers, where workers[i] represents the skill level of the i-th worker. You are also given a 2D integer array tasks, where:

  • tasks[i][0] represents the skill requirement needed to complete the task
  • tasks[i][1] represents the profit earned from completing the task

Each worker can complete at most one task, and they can only take a task if their skill level is equal to the task's skill requirement. An additional worker joins today who can take up any task, regardless of the skill requirement.

Return the maximum total profit that can be earned by optimally assigning the tasks to the workers.

Input & Output

Example 1 — Basic Assignment
$ Input: workers = [3,1,2], tasks = [[1,7],[2,4],[3,5]]
Output: 16
💡 Note: Worker with skill 1 takes task [1,7] for profit 7, worker with skill 2 takes task [2,4] for profit 4, worker with skill 3 takes task [3,5] for profit 5. Total profit = 7 + 4 + 5 = 16
Example 2 — Super Worker Needed
$ Input: workers = [1,1], tasks = [[1,5],[2,6],[1,3]]
Output: 14
💡 Note: Two workers with skill 1 can take tasks [1,5] and [1,3] for profit 5+3=8. Super worker takes remaining task [2,6] for profit 6. Total = 8 + 6 = 14
Example 3 — More Tasks Than Workers
$ Input: workers = [2], tasks = [[1,8],[2,7],[3,6]]
Output: 15
💡 Note: Regular worker with skill 2 takes task [2,7] for profit 7. Super worker takes highest remaining task [1,8] for profit 8. Total = 7 + 8 = 15

Constraints

  • 1 ≤ workers.length ≤ 105
  • 1 ≤ tasks.length ≤ 105
  • 1 ≤ workers[i], tasks[i][0] ≤ 105
  • 1 ≤ tasks[i][1] ≤ 108

Visualization

Tap to expand
Maximize Profit from Task AssignmentWorkers312SuperTasks [skill, profit][1,7][2,4][3,5]Optimal Assignment:Worker(skill=1) → Task[1,7] = Profit 7Worker(skill=2) → Task[2,4] = Profit 4Worker(skill=3) → Task[3,5] = Profit 5Total Profit: 16
Understanding the Visualization
1
Input
Workers with skill levels and tasks with skill requirements and profits
2
Process
Match workers to tasks optimally, using super worker for any unmatched task
3
Output
Maximum total profit achievable
Key Takeaway
🎯 Key Insight: Sort tasks by profit and greedily assign to workers with matching skills, using the super worker for the best remaining task
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
34.2K 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