Skip to content

Commit 1d20a2c

Browse files
committed
Sync LeetCode submission Runtime - 1673 ms (15.67%), Memory - 26.4 MB (23.51%)
1 parent 1ab0b3b commit 1d20a2c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<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>
2+
3+
<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>
4+
5+
<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>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<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
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong>
14+
We can assign the magical pill and tasks as follows:
15+
- Give the magical pill to worker 0.
16+
- Assign worker 0 to task 2 (0 + 1 &gt;= 1)
17+
- Assign worker 1 to task 1 (3 &gt;= 2)
18+
- Assign worker 2 to task 0 (3 &gt;= 3)
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> tasks = [<u><strong>5</strong></u>,4], workers = [<u><strong>0</strong></u>,0,0], pills = 1, strength = 5
25+
<strong>Output:</strong> 1
26+
<strong>Explanation:</strong>
27+
We can assign the magical pill and tasks as follows:
28+
- Give the magical pill to worker 0.
29+
- Assign worker 0 to task 0 (0 + 5 &gt;= 5)
30+
</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<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
36+
<strong>Output:</strong> 2
37+
<strong>Explanation:</strong>
38+
We can assign the magical pills and tasks as follows:
39+
- Give the magical pill to worker 0 and worker 1.
40+
- Assign worker 0 to task 0 (0 + 10 &gt;= 10)
41+
- Assign worker 1 to task 1 (10 + 10 &gt;= 15)
42+
The last pill is not given because it will not make any worker strong enough for the last task.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>n == tasks.length</code></li>
50+
<li><code>m == workers.length</code></li>
51+
<li><code>1 &lt;= n, m &lt;= 5 * 10<sup>4</sup></code></li>
52+
<li><code>0 &lt;= pills &lt;= m</code></li>
53+
<li><code>0 &lt;= tasks[i], workers[j], strength &lt;= 10<sup>9</sup></code></li>
54+
</ul>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from sortedcontainers import SortedList
2+
3+
class Solution:
4+
def maxTaskAssign(self, tasks: List[int], workers: List[int], pills: int, strength: int) -> int:
5+
n, m = len(tasks), len(workers)
6+
tasks.sort()
7+
workers.sort()
8+
9+
def check(mid):
10+
p = pills
11+
ws = SortedList(workers[m - mid :])
12+
13+
# Enumerate each task from largest to smallest
14+
for i in range(mid - 1, -1, -1):
15+
if ws[-1] >= tasks[i]:
16+
ws.pop()
17+
else:
18+
if p == 0:
19+
return False
20+
rep = ws.bisect_left(tasks[i] - strength)
21+
if rep == len(ws):
22+
return False
23+
p -= 1
24+
ws.pop(rep)
25+
return True
26+
27+
left, right, ans = 1, min(m, n), 0
28+
while left <= right:
29+
mid = (left + right) // 2
30+
if check(mid):
31+
ans = mid
32+
left = mid + 1
33+
else:
34+
right = mid - 1
35+
36+
return ans
37+
38+

0 commit comments

Comments
 (0)