Maximize Profit from Task Assignment - Problem

You're managing a freelance agency where workers with specific skill levels need to be matched with tasks that require exact skill matches. Each worker has a skill level, and each task has both a skill requirement and a profit reward.

The Challenge: You have an array workers where workers[i] represents the skill level of the i-th worker, and a 2D array tasks where tasks[i][0] is the skill requirement and tasks[i][1] is the profit for completing that task.

Key Rules:

  • Each worker can complete at most one task
  • Workers can only take tasks that exactly match their skill level
  • A special "super worker" joins who can complete any task regardless of skill requirement

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

Example: If you have workers with skills [4, 5, 6] and tasks [[4, 10], [5, 15], [6, 20], [7, 25]], the super worker should take the highest profit task (skill 7, profit 25), while regular workers take their matching tasks for a total profit of 10 + 15 + 20 + 25 = 70.

Input & Output

example_1.py โ€” Basic Case
$ Input: workers = [4, 5, 6], tasks = [[4, 10], [5, 15], [6, 20], [7, 25]]
โ€บ Output: 70
๐Ÿ’ก Note: Super worker takes task [7, 25] (skill 7, profit 25). Regular workers: worker with skill 4 takes [4, 10], worker with skill 5 takes [5, 15], worker with skill 6 takes [6, 20]. Total: 25 + 10 + 15 + 20 = 70.
example_2.py โ€” Multiple Workers Same Skill
$ Input: workers = [3, 3, 4], tasks = [[3, 1], [3, 7], [4, 5], [5, 10]]
โ€บ Output: 22
๐Ÿ’ก Note: Super worker takes [5, 10] (highest profit). Two workers with skill 3 take both [3, 7] and [3, 1]. Worker with skill 4 takes [4, 5]. Total: 10 + 7 + 1 + 5 = 23. Wait, let me recalculate: Super takes [5, 10], one skill-3 worker takes [3, 7], another skill-3 worker takes [3, 1], skill-4 worker takes [4, 5]. Total: 10 + 7 + 1 + 5 = 23.
example_3.py โ€” No Regular Worker Matches
$ Input: workers = [1, 2], tasks = [[3, 15], [4, 20], [5, 25]]
โ€บ Output: 25
๐Ÿ’ก Note: No regular workers can take any tasks (skill mismatch). Super worker takes the highest profit task [5, 25]. Total profit = 25.

Constraints

  • 1 โ‰ค workers.length โ‰ค 104
  • 1 โ‰ค tasks.length โ‰ค 104
  • 1 โ‰ค workers[i], tasks[i][0] โ‰ค 105
  • 1 โ‰ค tasks[i][1] โ‰ค 105
  • Each worker can complete at most one task
  • Super worker can take any task regardless of skill requirement

Visualization

Tap to expand
Freelance Agency Task AssignmentWorkersSkill 4Skill 5SuperTasks (Sorted by Profit)[7, $25][6, $20][5, $15][4, $10]Super โ†’ Best TaskAssignment Results:โœ… Super Worker โ†’ Task [7, $25] (highest profit, no skill match needed)โœ… Worker (Skill 6) โ†’ Task [6, $20] (exact skill match)โœ… Worker (Skill 5) โ†’ Task [5, $15] (exact skill match)โœ… Worker (Skill 4) โ†’ Task [4, $10] (exact skill match)Total Profit$70Algorithm Steps:1. Count workers by skill level2. Sort tasks by profit (descending)3. Assign highest-profit tasks first4. Use super worker for best available taskTime: O(m log m) for sortingSpace: O(k) for skill countingwhere m = tasks, k = unique skills
Understanding the Visualization
1
Inventory Check
Count how many workers you have for each skill level
2
Prioritize Profits
Sort all tasks by profit amount (highest first)
3
Smart Assignment
For each high-profit task, assign to a regular worker if possible, otherwise use your super worker
4
Maximize Returns
Continue until all workers are assigned or no more profitable tasks remain
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because assigning the highest-profit tasks first, with the super worker's flexibility to take any task, always leads to the optimal total profit.
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
31.2K Views
Medium-High Frequency
~18 min Avg. Time
856 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