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.

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

Visualization

Tap to expand
Maximum Number of Tasks You Can Assign INPUT Tasks (strength req): 3 2 1 Workers (strength): 0 3 1 Pills: 1 Strength boost: +3 Worker with pill: 0 + 3 = 3 strength 1 + 3 = 4 strength W1 W2 W3 GREEDY ALGORITHM 1 Sort Arrays Tasks: [1,2,3] Workers: [0,1,3] 2 Binary Search Find max tasks possible Try k=1,2,3... 3 Greedy Match Easiest tasks first Strongest workers first 4 Use Pills Wisely Give pill only when worker cant do task Assignments: W3(3) --> T3(3) W2(1+3) --> T2(2) W1(0+3) --> T1(1) FINAL RESULT Maximum Tasks: 3 Final Assignments: T1 W1 +3 T2 W2 1 T3 W3 3 All 3 tasks completed! 1 pill used optimally Key Insight: Use binary search to find the maximum number of tasks k. For each k, greedily assign the k easiest tasks to the k strongest workers. When a worker cannot complete a task naturally, use a pill if available. Process tasks from hardest to easiest within the k selected, maximizing the value of each pill used. TutorialsPoint - Maximum Number of Tasks You Can Assign | Greedy Approach
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
28.6K 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