<p>You have <code>n</code> tasks and <code>m</code> workers. Each task has a strength requirement stored in a <strong>0-indexed</strong> integer array <code>tasks</code>, with the <code>i<sup>th</sup></code> task requiring <code>tasks[i]</code> strength to complete. The strength of each worker is stored in a <strong>0-indexed</strong> integer array <code>workers</code>, with the <code>j<sup>th</sup></code> worker having <code>workers[j]</code> strength. Each worker can only be assigned to a <strong>single</strong> task and must have a strength <strong>greater than or equal</strong> to the task&#39;s strength requirement (i.e., <code>workers[j] &gt;= tasks[i]</code>).</p>

<p>Additionally, you have <code>pills</code> magical pills that will <strong>increase a worker&#39;s strength</strong> by <code>strength</code>. You can decide which workers receive the magical pills, however, you may only give each worker <strong>at most one</strong> magical pill.</p>

<p>Given the <strong>0-indexed </strong>integer arrays <code>tasks</code> and <code>workers</code> and the integers <code>pills</code> and <code>strength</code>, return <em>the <strong>maximum</strong> number of tasks that can be completed.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> tasks = [<u><strong>3</strong></u>,<u><strong>2</strong></u>,<u><strong>1</strong></u>], workers = [<u><strong>0</strong></u>,<u><strong>3</strong></u>,<u><strong>3</strong></u>], pills = 1, strength = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong>
We can assign the magical pill and tasks as follows:
- Give the magical pill to worker 0.
- Assign worker 0 to task 2 (0 + 1 &gt;= 1)
- Assign worker 1 to task 1 (3 &gt;= 2)
- Assign worker 2 to task 0 (3 &gt;= 3)
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> tasks = [<u><strong>5</strong></u>,4], workers = [<u><strong>0</strong></u>,0,0], pills = 1, strength = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong>
We can assign the magical pill and tasks as follows:
- Give the magical pill to worker 0.
- Assign worker 0 to task 0 (0 + 5 &gt;= 5)
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> tasks = [<u><strong>10</strong></u>,<u><strong>15</strong></u>,30], workers = [<u><strong>0</strong></u>,<u><strong>10</strong></u>,10,10,10], pills = 3, strength = 10
<strong>Output:</strong> 2
<strong>Explanation:</strong>
We can assign the magical pills and tasks as follows:
- Give the magical pill to worker 0 and worker 1.
- Assign worker 0 to task 0 (0 + 10 &gt;= 10)
- Assign worker 1 to task 1 (10 + 10 &gt;= 15)
The last pill is not given because it will not make any worker strong enough for the last task.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == tasks.length</code></li>
	<li><code>m == workers.length</code></li>
	<li><code>1 &lt;= n, m &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= pills &lt;= m</code></li>
	<li><code>0 &lt;= tasks[i], workers[j], strength &lt;= 10<sup>9</sup></code></li>
</ul>

# class Solution:
#     def maxTaskAssign(self, tasks: List[int], workers: List[int], pills: int, strength: int) -> int:
#         from collections import deque 
#         sortedTasks=sorted(tasks)
#         sortedWorkers=sorted(workers)

#         def valid(m,pills):
#             cur_workers = deque(sortedWorkers[-m:])
#             #store got pilled but not used worker
#             pilled=deque()
#             #store not got pilled and not used worker
#             not_pilled=deque()
#             p = pills
#             for i in range(m):
#                 task = sortedTasks[i]
#                 # pilled all worker cannot finish this task
#                 while cur_workers and cur_workers[0] < task:
#                     if pills == 0 or cur_workers[0]+strength < task:
#                         return False
#                     pilled.append(cur_workers.popleft() + strength)
#                     pills -= 1
                
#                 while not_pilled and (not_pilled[0]+ strength  < task or pills == 0):
#                     not_pilled.popleft()
#                 if pilled and (len(cur_workers)==0 or pilled[0] <=cur_workers[0]):
#                     if pilled[0] >= task:
#                         pilled.popleft()
#                     elif not_pilled and p>0:
#                         not_pilled.popleft()
#                         pilled.popleft()
#                         p -= 1
#                 elif cur_workers:
#                     not_pilled.append(cur_workers.popleft())
#                 else:
#                     return False 
#             return True
                



#         l, r = 1, min(len(sortedTasks), len(sortedWorkers)) + 1
#         while l < r:
#             m = (l + r) // 2
#             if valid(m,pills):
#                 l = m + 1
#             else:
#                 r = m
#         return l - 1

class Solution:
    def maxTaskAssign(self, tasks: List[int], workers: List[int], pills: int, strength: int) -> int:
        tasks.sort()
        workers.sort()

        def valid(m):
            wrkrs = deque(workers[-m:])
            pilled = deque()
            not_pilled = deque()
            p = pills
            for t in tasks[:m]:
                while wrkrs and wrkrs[0] < t:
                    if p == 0 or wrkrs[0] + strength < t:
                        return False
                    pilled.append(wrkrs.popleft() + strength)
                    p -= 1
                while not_pilled and (p == 0 or not_pilled[0] + strength < t):
                    not_pilled.popleft()
                if pilled and ((not wrkrs) or pilled[0] <= wrkrs[0]):
                    if pilled[0] >= t:
                        pilled.popleft()
                    elif not_pilled and p > 0:
                        not_pilled.popleft()
                        pilled.popleft()
                        p -= 1
                    else:
                        return False
                elif wrkrs:
                    not_pilled.append(wrkrs.popleft())
                else:
                    return False                
            return True

        l, r = 1, min(len(tasks), len(workers)) + 1
        while l < r:
            m = (l + r) // 2
            if valid(m):
                l = m + 1
            else:
                r = m
        return l - 1
25800