Skip to content

Commit 66c8030

Browse files
authored
Create 2071. Maximum Number of Tasks You Can Assign
1 parent 10da286 commit 66c8030

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {
4+
vector<int> task_requirements = tasks;
5+
vector<int> worker_strengths = workers;
6+
int total_pills = pills;
7+
int pill_boost = strength;
8+
sort(task_requirements.begin(), task_requirements.end());
9+
sort(worker_strengths.begin(), worker_strengths.end());
10+
11+
auto can_assign = [&](int k) {
12+
vector<int> avail(worker_strengths.end() - k, worker_strengths.end());
13+
int pills_remain = total_pills;
14+
for (int i = k - 1; i >= 0; --i) {
15+
int req = task_requirements[i];
16+
if (!avail.empty() && avail.back() >= req) {
17+
avail.pop_back();
18+
} else {
19+
if (pills_remain <= 0) return false;
20+
int threshold = req - pill_boost;
21+
auto it = lower_bound(avail.begin(), avail.end(), threshold);
22+
if (it == avail.end()) return false;
23+
avail.erase(it);
24+
--pills_remain;
25+
}
26+
}
27+
return true;
28+
};
29+
30+
int low = 0, high = min((int)tasks.size(), (int)workers.size()), completed = 0;
31+
while (low <= high) {
32+
int mid = (low + high) / 2;
33+
if (can_assign(mid)) {
34+
completed = mid;
35+
low = mid + 1;
36+
} else {
37+
high = mid - 1;
38+
}
39+
}
40+
return completed;
41+
}
42+
};

0 commit comments

Comments
 (0)