Leetcode Problem 2071. Maximum Number of Tasks You Can Assign
2071. Maximum Number of Tasks You Can Assign
Leetcode Solutions
Binary Search + Greedy Allocation
Sort the tasks and workers arrays in non-decreasing order.
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.
For each mid value in the binary search, attempt to assign mid tasks to mid workers.
Use a greedy approach to assign tasks to workers without using pills first.
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.
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.
Adjust the binary search space based on whether the current mid value is possible or not.
Return the maximum number of tasks that can be completed.