Class Solution
java.lang.Object
g2001_2100.s2071_maximum_number_of_tasks_you_can_assign.Solution
2071 - Maximum Number of Tasks You Can Assign.<p>Hard</p>
<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’s strength requirement (i.e., <code>workers[j] >= tasks[i]</code>).</p>
<p>Additionally, you have <code>pills</code> magical pills that will <strong>increase a worker’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><strong>Example 1:</strong></p>
<p><strong>Input:</strong> tasks = [<strong>3</strong> , <strong>2</strong> , <strong>1</strong> ], workers = [<strong>0</strong> , <strong>3</strong> , <strong>3</strong> ], pills = 1, strength = 1</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<p>We can assign the magical pill and tasks as follows:</p>
<ul>
<li>
<p>Give the magical pill to worker 0.</p>
</li>
<li>
<p>Assign worker 0 to task 2 (0 + 1 >= 1)</p>
</li>
<li>
<p>Assign worker 1 to task 1 (3 >= 2)</p>
</li>
<li>
<p>Assign worker 2 to task 0 (3 >= 3)</p>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> tasks = [<strong>5</strong> ,4], workers = [<strong>0</strong> ,0,0], pills = 1, strength = 5</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>We can assign the magical pill and tasks as follows:</p>
<ul>
<li>
<p>Give the magical pill to worker 0.</p>
</li>
<li>
<p>Assign worker 0 to task 0 (0 + 5 >= 5)</p>
</li>
</ul>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> tasks = [<strong>10</strong> , <strong>15</strong> ,30], workers = [<strong>0</strong> , <strong>10</strong> ,10,10,10], pills = 3, strength = 10</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>We can assign the magical pills and tasks as follows:</p>
<ul>
<li>
<p>Give the magical pill to worker 0 and worker 1.</p>
</li>
<li>
<p>Assign worker 0 to task 0 (0 + 10 >= 10)</p>
</li>
<li>
<p>Assign worker 1 to task 1 (10 + 10 >= 15)</p>
</li>
</ul>
<p>The last pill is not given because it will not make any worker strong enough for the last task.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tasks.length</code></li>
<li><code>m == workers.length</code></li>
<li><code>1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li><code>0 <= pills <= m</code></li>
<li><code>0 <= tasks[i], workers[j], strength <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintmaxTaskAssign(int[] tasks, int[] workers, int pills, int strength)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxTaskAssign
public int maxTaskAssign(int[] tasks, int[] workers, int pills, int strength)
-