Assign the maximum number of tasks to workers using optional pills that boost worker strength.
You are given a list of task requirements and a list of workers. Each task requires a certain strength to complete, and each worker has a base strength. You may give a limited number of pills, each of which temporarily increases one worker's strength by a fixed amount. A worker can complete at most one task, and each task can be assigned to at most one worker.
Your goal is to determine the maximum number of tasks that can be completed if you assign tasks and pills optimally.
This is a classic feasibility problem: decide whether a given number of tasks can be assigned, then use that check to find the maximum possible count.
Input Format
- An integer array
taskswheretasks[i]is the strength needed for thei-th task. - An integer array
workerswhereworkers[j]is the base strength of thej-th worker. - Two integers
pillsandstrength.
A pill can be used on at most one worker and increases that worker's strength by strength for the assignment.
Output Format
Return the maximum number of tasks that can be completed.
Constraints
- A worker can complete at most one task.
- A task can be assigned to at most one worker.
- At most
pillsworkers may receive a pill. - The answer is the largest integer
ksuch that somektasks can be assigned successfully.
Example 1
Input
tasks = [3,2,1] workers = [0,3,3] pills = 1 strength = 1
Output
3
Explanation
Assign task 1 to a worker with strength 3, task 2 to the other strength-3 worker, and use the pill on the worker with strength 0 to finish task 3.
Example 2
Input
tasks = [5,4] workers = [0,0,0] pills = 2 strength = 3
Output
0
Explanation
Even with pills, each worker reaches strength 3, which is still not enough for either task.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.