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
Visualization
Time & Space Complexity
O(n log n) for sorting, O(log n) for binary search, O(n) for each validation
Space for sorted arrays and deque for worker management
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