[LeetCode] Maximum Number of Tasks You Can Assign

2071. Maximum Number of Tasks You Can Assign

You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the jth 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]).

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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#define all(a) begin(a), end(a)

class Solution {
public:
int maxTaskAssign(vector<int>& t, vector<int>& w, int p, int s) {
sort(all(w));
sort(all(t));
int l = 0, r = min(t.size(), w.size());
while(l <= r) {
int m = (l + r) >> 1, use = 0;
multiset<int> ms(end(w) - m, end(w));

for(int i = m - 1; i >= 0; i--) {
auto wo = prev(end(ms));
if(*wo < t[i]) {
wo = ms.lower_bound(t[i] - s);
if(wo == end(ms) or ++use > p) break;
}
ms.erase(wo);
}

if(ms.empty()) l = m + 1;
else r = m - 1;
}
return r;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/25/PS/LeetCode/maximum-number-of-tasks-you-can-assign/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.