2071. Maximum Number of Tasks You Can Assign #1629
-
Topics: You have Additionally, you have Given the 0-indexed integer arrays Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the maximum number of tasks that can be assigned to workers given that each worker can use a pill to boost their strength. The solution involves using a binary search approach combined with a greedy algorithm to efficiently check if a certain number of tasks can be completed. Approach
Let's implement this solution in PHP: 2071. Maximum Number of Tasks You Can Assign <?php
/**
* @param Integer[] $tasks
* @param Integer[] $workers
* @param Integer $pills
* @param Integer $strength
* @return Integer
*/
public function maxTaskAssign($tasks, $workers, $pills, $strength) {
sort($tasks);
sort($workers);
$ans = 0;
$left = 0;
$right = min(count($tasks), count($workers));
while ($left <= $right) {
$mid = (int)(($left + $right) / 2);
if ($this->canComplete($tasks, $workers, $pills, $strength, $mid)) {
$ans = $mid;
$left = $mid + 1;
} else {
$right = $mid - 1;
}
}
return $ans;
}
/**
* @param $tasks
* @param $workers
* @param $pills
* @param $strength
* @param $m
* @return bool
*/
private function canComplete($tasks, $workers, $pills, $strength, $m) {
if ($m == 0) {
return true;
}
if (count($workers) < $m) {
return false;
}
$selected_tasks = array_slice($tasks, 0, $m);
$selected_tasks = array_reverse($selected_tasks);
$selected_workers = array_slice($workers, -$m);
sort($selected_workers); // Ensure they are sorted in ascending order
$pills_remaining = $pills;
foreach ($selected_tasks as $task) {
$index = $this->findCeiling($selected_workers, $task);
if ($index !== -1) {
array_splice($selected_workers, $index, 1);
} else {
if ($pills_remaining <= 0) {
return false;
}
$required = $task - $strength;
$index = $this->findCeiling($selected_workers, $required);
if ($index !== -1) {
array_splice($selected_workers, $index, 1);
$pills_remaining--;
} else {
return false;
}
}
}
return true;
}
/**
* @param $arr
* @param $target
* @return int
*/
private function findCeiling($arr, $target) {
$left = 0;
$right = count($arr) - 1;
$result = -1;
while ($left <= $right) {
$mid = (int)(($left + $right) / 2);
if ($arr[$mid] >= $target) {
$result = $mid;
$right = $mid - 1;
} else {
$left = $mid + 1;
}
}
return $result;
}
// Example usage
$tasks = [3, 2, 1];
$workers = [0, 3, 3];
$pills = 1;
$strength = 1;
echo maxTaskAssign($tasks, $workers, $pills, $strength) . "\n"; // Output: 3
$tasks = [5,4];
$workers = [0,0,0];
$pills = 1;
$strength = 5;
echo maxTaskAssign($tasks, $workers, $pills, $strength) . "\n"; // Output: 1
$tasks = [10,15,30];
$workers = [0,10,10,10,10];
$pills = 3;
$strength = 10;
echo maxTaskAssign($tasks, $workers, $pills, $strength) . "\n"; // Output: 2
$tasks = [5,9,8,5,9];
$workers = [1,6,4,2,6];
$pills = 1;
$strength = 5;
echo maxTaskAssign($tasks, $workers, $pills, $strength) . "\n"; // Output: 3
?> Explanation:
This approach ensures that we efficiently check each possible number of tasks using binary search and a greedy algorithm, leading to an optimal solution. |
Beta Was this translation helpful? Give feedback.
We need to determine the maximum number of tasks that can be assigned to workers given that each worker can use a pill to boost their strength. The solution involves using a binary search approach combined with a greedy algorithm to efficiently check if a certain number of tasks can be completed.
Approach