bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2071. Maximum Number of Tasks You Can Assign

2071. Maximum Number of Tasks You Can Assign

Leetcode Solutions

Binary Search + Greedy Allocation

  1. Sort the tasks and workers arrays in non-decreasing order.
  2. Perform binary search on the number of tasks that can be completed, with the search space ranging from 0 to the minimum of the length of tasks and workers.
  3. For each mid value in the binary search, attempt to assign mid tasks to mid workers.
  4. Use a greedy approach to assign tasks to workers without using pills first.
  5. If a task cannot be assigned without a pill, use a pill on the weakest worker that can complete the task with the added strength.
  6. If the number of pills used exceeds pills, or if a task cannot be assigned even with a pill, the current mid value is not possible.
  7. Adjust the binary search space based on whether the current mid value is possible or not.
  8. Return the maximum number of tasks that can be completed.
UML Thumbnail

Greedy Allocation with Sorting and Iteration

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...