Maximum Number of Tasks You Can Assign - Problem

Imagine you're a project manager with n tasks to complete and m workers available. Each task has a specific strength requirement, and each worker has their own strength level. A worker can only handle a task if their strength is greater than or equal to the task's requirement.

Here's the twist: you have pills magical energy boosters that can increase any worker's strength by strength points! Each worker can receive at most one pill, and you need to decide strategically who gets them.

Goal: Determine the maximum number of tasks you can complete by optimally assigning workers to tasks and distributing the magical pills.

Input: Two arrays tasks[] and workers[] representing strength requirements and worker strengths, plus integers pills and strength.

Output: Maximum number of tasks that can be completed.

Input & Output

example_1.py — Basic Case
$ Input: {"tasks": [3,2,1], "workers": [0,3,1], "pills": 1, "strength": 3}
Output: 3
💡 Note: We can complete all 3 tasks: assign worker with strength 3 to task requiring 3, worker with strength 1 to task requiring 1, and give a pill to worker with strength 0 (becomes 3) to handle task requiring 2.
example_2.py — Limited Pills
$ Input: {"tasks": [5,4], "workers": [0,0,0], "pills": 1, "strength": 5}
Output: 1
💡 Note: We have 3 workers each with 0 strength, but only 1 pill that adds 5 strength. So only one worker can be enhanced to strength 5, allowing completion of only 1 task.
example_3.py — No Pills Needed
$ Input: {"tasks": [1,1,1], "workers": [10,10,10], "pills": 10, "strength": 10}
Output: 3
💡 Note: All workers are already strong enough for all tasks, so we can complete all 3 tasks without using any pills.

Visualization

Tap to expand
Maximum Task Assignment StrategyQuests (sorted by difficulty):Easy (1)Med (3)Hard (5)Heroes (sorted by power):Weak (0)Mid (2)Strong (4)Power Potions Available: 1 (+3 power)+3Binary Search Process:0-3?Can we complete 0-3 quests?✓ Try k=2: Easy→Strong(4), Med→Mid(2)? No, need potion✓ Try k=2: Easy→Strong(4), Med→Weak+Potion(3) ✓2Answer: Can complete 2 quests maximumGreedy Strategy:1. Always try easiest remaining quests first2. Use strongest heroes without potions when possible3. Save potions for when absolutely necessary4. Binary search finds exact maximum efficientlyFinal Assignment:Easy→StrongMed→Weak+PotHard❌Completed: 2/3 quests (optimal!)
Understanding the Visualization
1
Sort by Difficulty
Arrange tasks from easiest to hardest, and workers from weakest to strongest
2
Binary Search Answer
Use binary search to find the maximum number of tasks we can complete
3
Greedy Validation
For each candidate answer, try to complete the easiest k tasks using strongest workers first
4
Strategic Pill Usage
Only use pills when a normal assignment would fail, finding the weakest worker who can handle the task with a pill
Key Takeaway
🎯 Key Insight: Binary search on the answer combined with greedy assignment ensures we find the maximum tasks efficiently. Always tackle easiest tasks first and use pills strategically!

Time & Space Complexity

Time Complexity
⏱️
O(n log n log n)

O(n log n) for sorting, O(log n) for binary search, O(n) for each validation

n
2n
Linearithmic
Space Complexity
O(n)

Space for sorted arrays and deque for worker management

n
2n
Linearithmic Space

Constraints

  • n == tasks.length
  • m == workers.length
  • 1 ≤ n, m ≤ 5 × 104
  • 0 ≤ pills ≤ m
  • 0 ≤ tasks[i], workers[j], strength ≤ 109
  • Each worker can be assigned to at most one task
  • Each worker can receive at most one pill
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
28.5K Views
Medium Frequency
~35 min Avg. Time
847 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