Maximum Number of Tasks You Can Assign

You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the i^th task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the j^th worker having workers[j] strength.

Each worker:

  • Can only be assigned to a single task.
  • Must have strength greater than or equal to the task's strength requirement, meaning workers[j] >= tasks[i].

Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, but you may only give each worker at most one magical pill.

Given the 0-indexed integer arrays tasks and workers, and the integers pills and strength, return the maximum number of tasks that can be completed.

Example 1
Inputtasks = [3,2,1], workers = [0,3,3], pills = 1, strength = 1
Output3
Giving the magical pill to worker 0 lets them complete task 2, while workers 1 and 2 complete tasks 1 and 0, so all 3 tasks can be completed.
Example 2
Inputtasks = [5,4], workers = [0,0,0], pills = 1, strength = 5
Output1
Giving the magical pill to worker 0 lets them complete task 0, so 1 task can be completed.

Constraints

  • n == tasks.length
  • m == workers.length
  • 1 <= n, m <= 5 * 10^4
  • 0 <= pills <= m
  • 0 <= tasks[i], workers[j], strength <= 10^9

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate