Skip to content

Commit b98c847

Browse files
authored
Added task 2071.
1 parent c24ee30 commit b98c847

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g2001_2100.s2071_maximum_number_of_tasks_you_can_assign;
2+
3+
// #Hard #Array #Sorting #Greedy #Binary_Search #Queue #Monotonic_Queue
4+
// #2022_05_30_Time_156_ms_(40.49%)_Space_117.1_MB_(61.46%)
5+
6+
import java.util.ArrayDeque;
7+
import java.util.Arrays;
8+
import java.util.Deque;
9+
10+
public class Solution {
11+
public int maxTaskAssign(int[] tasks, int[] workers, int pills, int strength) {
12+
int left = 0;
13+
int right = Math.min(tasks.length, workers.length);
14+
Arrays.sort(tasks);
15+
Arrays.sort(workers);
16+
while (left + 1 < right) {
17+
int mid = left + (right - left) / 2;
18+
if (canAssign(mid, tasks, workers, pills, strength)) {
19+
left = mid;
20+
} else {
21+
right = mid;
22+
}
23+
}
24+
if (canAssign(right, tasks, workers, pills, strength)) {
25+
return right;
26+
} else {
27+
return left;
28+
}
29+
}
30+
31+
private boolean canAssign(int count, int[] tasks, int[] workers, int pills, int strength) {
32+
Deque<Integer> dq = new ArrayDeque<>();
33+
int ind = workers.length - 1;
34+
for (int i = count - 1; i >= 0; i--) {
35+
while (ind >= workers.length - count && workers[ind] + strength >= tasks[i]) {
36+
dq.offerLast(workers[ind]);
37+
ind--;
38+
}
39+
if (dq.isEmpty()) {
40+
return false;
41+
}
42+
if (dq.peekFirst() >= tasks[i]) {
43+
dq.pollFirst();
44+
} else {
45+
dq.pollLast();
46+
pills--;
47+
if (pills < 0) {
48+
return false;
49+
}
50+
}
51+
}
52+
return true;
53+
}
54+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2071\. Maximum Number of Tasks You Can Assign
2+
3+
Hard
4+
5+
You have `n` tasks and `m` workers. Each task has a strength requirement stored in a **0-indexed** integer array `tasks`, with the <code>i<sup>th</sup></code> task requiring `tasks[i]` strength to complete. The strength of each worker is stored in a **0-indexed** integer array `workers`, with the <code>j<sup>th</sup></code> worker having `workers[j]` strength. Each worker can only be assigned to a **single** task and must have a strength **greater than or equal** to the task's strength requirement (i.e., `workers[j] >= tasks[i]`).
6+
7+
Additionally, you have `pills` magical pills that will **increase a worker's strength** by `strength`. You can decide which workers receive the magical pills, however, you may only give each worker **at most one** magical pill.
8+
9+
Given the **0-indexed** integer arrays `tasks` and `workers` and the integers `pills` and `strength`, return _the **maximum** number of tasks that can be completed._
10+
11+
**Example 1:**
12+
13+
**Input:** tasks = [**3**,**2**,**1**], workers = [**0**,**3**,**3**], pills = 1, strength = 1
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
We can assign the magical pill and tasks as follows:
20+
21+
- Give the magical pill to worker 0.
22+
23+
- Assign worker 0 to task 2 (0 + 1 >= 1)
24+
25+
- Assign worker 1 to task 1 (3 >= 2)
26+
27+
- Assign worker 2 to task 0 (3 >= 3)
28+
29+
**Example 2:**
30+
31+
**Input:** tasks = [**5**,4], workers = [**0**,0,0], pills = 1, strength = 5
32+
33+
**Output:** 1
34+
35+
**Explanation:**
36+
37+
We can assign the magical pill and tasks as follows:
38+
39+
- Give the magical pill to worker 0.
40+
41+
- Assign worker 0 to task 0 (0 + 5 >= 5)
42+
43+
**Example 3:**
44+
45+
**Input:** tasks = [**10**,**15**,30], workers = [**0**,**10**,10,10,10], pills = 3, strength = 10
46+
47+
**Output:** 2
48+
49+
**Explanation:**
50+
51+
We can assign the magical pills and tasks as follows:
52+
53+
- Give the magical pill to worker 0 and worker 1.
54+
55+
- Assign worker 0 to task 0 (0 + 10 >= 10)
56+
57+
- Assign worker 1 to task 1 (10 + 10 >= 15)
58+
59+
The last pill is not given because it will not make any worker strong enough for the last task.
60+
61+
**Constraints:**
62+
63+
* `n == tasks.length`
64+
* `m == workers.length`
65+
* <code>1 <= n, m <= 5 * 10<sup>4</sup></code>
66+
* `0 <= pills <= m`
67+
* <code>0 <= tasks[i], workers[j], strength <= 10<sup>9</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2001_2100.s2071_maximum_number_of_tasks_you_can_assign;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxTaskAssign() {
11+
assertThat(
12+
new Solution().maxTaskAssign(new int[] {3, 2, 1}, new int[] {0, 3, 3}, 1, 1),
13+
equalTo(3));
14+
}
15+
16+
@Test
17+
void maxTaskAssign2() {
18+
assertThat(
19+
new Solution().maxTaskAssign(new int[] {5, 4}, new int[] {0, 0, 0}, 1, 5),
20+
equalTo(1));
21+
}
22+
23+
@Test
24+
void maxTaskAssign3() {
25+
assertThat(
26+
new Solution()
27+
.maxTaskAssign(
28+
new int[] {10, 15, 30}, new int[] {0, 10, 10, 10, 10}, 3, 10),
29+
equalTo(2));
30+
}
31+
}

0 commit comments

Comments
 (0)