Skip to content

Commit 562547b

Browse files
authored
feat: add solutions to lc problem: No.2071 (#4382)
No.2071.Maximum Number of Tasks You Can Assign
1 parent 39eaf17 commit 562547b

File tree

3 files changed

+199
-1
lines changed

3 files changed

+199
-1
lines changed

solution/2000-2099/2071.Maximum Number of Tasks You Can Assign/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,69 @@ func maxTaskAssign(tasks []int, workers []int, pills int, strength int) int {
296296
}
297297
```
298298

299+
#### TypeScript
300+
301+
```ts
302+
function maxTaskAssign(
303+
tasks: number[],
304+
workers: number[],
305+
pills: number,
306+
strength: number,
307+
): number {
308+
tasks.sort((a, b) => a - b);
309+
workers.sort((a, b) => a - b);
310+
311+
const n = tasks.length;
312+
const m = workers.length;
313+
314+
const check = (x: number): boolean => {
315+
const dq = new Array<number>(x);
316+
let head = 0;
317+
let tail = 0;
318+
const empty = () => head === tail;
319+
const pushBack = (val: number) => {
320+
dq[tail++] = val;
321+
};
322+
const popFront = () => {
323+
head++;
324+
};
325+
const popBack = () => {
326+
tail--;
327+
};
328+
const front = () => dq[head];
329+
330+
let i = 0;
331+
let p = pills;
332+
333+
for (let j = m - x; j < m; j++) {
334+
while (i < x && tasks[i] <= workers[j] + strength) {
335+
pushBack(tasks[i]);
336+
i++;
337+
}
338+
339+
if (empty()) return false;
340+
341+
if (front() <= workers[j]) {
342+
popFront();
343+
} else {
344+
if (p === 0) return false;
345+
p--;
346+
popBack();
347+
}
348+
}
349+
return true;
350+
};
351+
352+
let [left, right] = [0, Math.min(n, m)];
353+
while (left < right) {
354+
const mid = (left + right + 1) >> 1;
355+
if (check(mid)) left = mid;
356+
else right = mid - 1;
357+
}
358+
return left;
359+
}
360+
```
361+
299362
<!-- tabs:end -->
300363

301364
<!-- solution:end -->

solution/2000-2099/2071.Maximum Number of Tasks You Can Assign/README_EN.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,21 @@ The last pill is not given because it will not make any worker strong enough for
8484

8585
<!-- solution:start -->
8686

87-
### Solution 1
87+
### Solution 1: Greedy + Binary Search
88+
89+
Sort the tasks in ascending order of completion time and the workers in ascending order of ability.
90+
91+
Suppose the number of tasks we want to assign is $x$. We can greedily assign the first $x$ tasks to the $x$ workers with the highest strength. If it is possible to complete $x$ tasks, then it is also possible to complete $x-1$, $x-2$, $x-3$, ..., $1$, $0$ tasks. Therefore, we can use binary search to find the maximum $x$ such that it is possible to complete $x$ tasks.
92+
93+
We define a function $check(x)$ to determine whether it is possible to complete $x$ tasks.
94+
95+
The implementation of $check(x)$ is as follows:
96+
97+
Iterate through the $x$ workers with the highest strength in ascending order. Let the current worker being processed be $j$. The current available tasks must satisfy $tasks[i] \leq workers[j] + strength$.
98+
99+
If the smallest required strength task $task[i]$ among the current available tasks is less than or equal to $workers[j]$, then worker $j$ can complete task $task[i]$ without using a pill. Otherwise, the current worker must use a pill. If there are pills remaining, use one pill and complete the task with the highest required strength among the current available tasks. Otherwise, return `false`.
100+
101+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of tasks.
88102

89103
<!-- tabs:start -->
90104

@@ -272,6 +286,69 @@ func maxTaskAssign(tasks []int, workers []int, pills int, strength int) int {
272286
}
273287
```
274288

289+
#### TypeScript
290+
291+
```ts
292+
function maxTaskAssign(
293+
tasks: number[],
294+
workers: number[],
295+
pills: number,
296+
strength: number,
297+
): number {
298+
tasks.sort((a, b) => a - b);
299+
workers.sort((a, b) => a - b);
300+
301+
const n = tasks.length;
302+
const m = workers.length;
303+
304+
const check = (x: number): boolean => {
305+
const dq = new Array<number>(x);
306+
let head = 0;
307+
let tail = 0;
308+
const empty = () => head === tail;
309+
const pushBack = (val: number) => {
310+
dq[tail++] = val;
311+
};
312+
const popFront = () => {
313+
head++;
314+
};
315+
const popBack = () => {
316+
tail--;
317+
};
318+
const front = () => dq[head];
319+
320+
let i = 0;
321+
let p = pills;
322+
323+
for (let j = m - x; j < m; j++) {
324+
while (i < x && tasks[i] <= workers[j] + strength) {
325+
pushBack(tasks[i]);
326+
i++;
327+
}
328+
329+
if (empty()) return false;
330+
331+
if (front() <= workers[j]) {
332+
popFront();
333+
} else {
334+
if (p === 0) return false;
335+
p--;
336+
popBack();
337+
}
338+
}
339+
return true;
340+
};
341+
342+
let [left, right] = [0, Math.min(n, m)];
343+
while (left < right) {
344+
const mid = (left + right + 1) >> 1;
345+
if (check(mid)) left = mid;
346+
else right = mid - 1;
347+
}
348+
return left;
349+
}
350+
```
351+
275352
<!-- tabs:end -->
276353

277354
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function maxTaskAssign(
2+
tasks: number[],
3+
workers: number[],
4+
pills: number,
5+
strength: number,
6+
): number {
7+
tasks.sort((a, b) => a - b);
8+
workers.sort((a, b) => a - b);
9+
10+
const n = tasks.length;
11+
const m = workers.length;
12+
13+
const check = (x: number): boolean => {
14+
const dq = new Array<number>(x);
15+
let head = 0;
16+
let tail = 0;
17+
const empty = () => head === tail;
18+
const pushBack = (val: number) => {
19+
dq[tail++] = val;
20+
};
21+
const popFront = () => {
22+
head++;
23+
};
24+
const popBack = () => {
25+
tail--;
26+
};
27+
const front = () => dq[head];
28+
29+
let i = 0;
30+
let p = pills;
31+
32+
for (let j = m - x; j < m; j++) {
33+
while (i < x && tasks[i] <= workers[j] + strength) {
34+
pushBack(tasks[i]);
35+
i++;
36+
}
37+
38+
if (empty()) return false;
39+
40+
if (front() <= workers[j]) {
41+
popFront();
42+
} else {
43+
if (p === 0) return false;
44+
p--;
45+
popBack();
46+
}
47+
}
48+
return true;
49+
};
50+
51+
let [left, right] = [0, Math.min(n, m)];
52+
while (left < right) {
53+
const mid = (left + right + 1) >> 1;
54+
if (check(mid)) left = mid;
55+
else right = mid - 1;
56+
}
57+
return left;
58+
}

0 commit comments

Comments
 (0)