Maximum Number of Tasks You Can Assign LeetCode solution in Python

1
class Solution:
2
def maxTaskAssign(
3
self, tasks: List[int], workers: List[int], pills: int, strength: int
4
) -> int:
5

6
from sortedcontainers import SortedList
7

8
tasks.sort()
9
workers.sort()
10

11
def check_valid(ans):
12

13
# _tasks = SortedList(tasks[:ans])
14
_tasks = deque(tasks[:ans])
15
_workers = workers[-ans:]
16
remain_pills = pills
17

18
for worker in _workers:
19
task = _tasks[0]
20
if worker >= task:
21
# the worker can finish the min task without pill, just move on
22
# _tasks.pop(0)
23
_tasks.popleft()
24
elif worker + strength >= task and remain_pills:
25
# the worker cannot finish the min task without pill, but can solve it with pill
26
# remove the max task that the strengthened worker can finish instead
27
# remove_task_idx = _tasks.bisect_right(worker + strength)
28
remove_task_idx = bisect.bisect_right(_tasks, worker + strength)
29
# _tasks.pop(remove_task_idx - 1)
30
del _tasks[remove_task_idx - 1]
31
remain_pills -= 1
32
else:
33
return False
34
return True
35

36
lo, hi = 0, min(len(workers), len(tasks))
37
while lo < hi:
38
mid = (lo + hi + 1) // 2
39
if check_valid(mid):
40
lo = mid
41
else:
42
hi = mid - 1
43
return lo

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0