Maximum Number of Tasks You Can Assign
Match workers to tasks, with a few pills that make a worker stronger, so that as many tasks as possible get done.
You are given two arrays of integers. tasks[i]iworkers[j]j
You also have pillsstrength
Example 1
Output: 3
Explanation: the worker with strength 0 takes the pill, reaches 1 and does the task that needs 1. The two workers with strength 3 do the tasks that need 2 and 3.
Example 2
Output: 1
Explanation: one worker takes the pill, reaches 5 and does one task. The other workers stay at 0 and can do nothing.
Example 3
Output: 2
Explanation: one worker with strength 10 does the task that needs 10. Another takes a pill, reaches 20 and does the task that needs 15. No worker reaches 30, even with a pill.
The Idea
Think of a small team and a list of jobs. Each job needs a certain strength, each person has a strength, and a few pills can make a person stronger. You want as many jobs done as possible, one person per job.
In Example 1 the tasks need 3, 2 and 1, and the workers have strengths 0, 3 and 3. The worker with 0 can do nothing alone. With the one pill, of strength 1, they reach 1 and can do the task that needs 1. The two workers with 3 take the tasks that need 2 and 3, and all three tasks are done.
With a few hundred workers, though, there are far too many ways to pair workers with tasks to try them all. So which tasks should we even aim for, and how many of them?
Suppose some plan gets k tasks done. Swap a task in it for an easier one that is not in it, and the plan still works, because whoever did the harder task can do the easier one. Swap a worker for a stronger one who has no task, and it still works too. So if any k tasks can be done, then the k easiest tasks can be done by the k strongest workers.
That turns the problem into a yes-or-no question for each k: can the k easiest tasks be done by the k strongest workers? And if k tasks can be done, so can k - 1: leave out one task and the worker who did it. So as k grows, the answers are yes, yes, yes, and then no from some point on. We need the last yes.
How do we answer the question for one k? Go through the k strongest workers, weakest first. If the weakest can do the easiest task without a pill, they take it: every other worker could do that task too, so it is the one least worth saving. If they cannot, they need a pill, and then the question is which task to give them.
Consider tasks [3, 5][2, 4]
In the top half, worker 2 spends the pill on the easiest task, 3. Worker 4 is left with task 5, which needs more than 4, and no pill remains, so only one task is done. In the bottom half, worker 2 spends the pill on the hardest task, 5, and worker 4 does task 3 without a pill. Both tasks are done.
So a worker who needs a pill should take the hardest task the pill puts in reach. The pill is spent either way, and the hard task is the one the other workers are least likely to manage. A worker who needs no pill takes the easiest task.
Solution Approach
Turning that idea into code means settling three things: which tasks and workers to use for a given k, how to find the largest k that works, and how to check one k quickly.
Choosing the tasks and workers
Why not try every way to pair workers with tasks? Each task could go to any free worker, with or without a pill, and the number of pairings grows faster than any power of the number of tasks. Even ten tasks and ten workers give millions of them.
Sorting both arrays avoids that. After sorting, the k easiest tasks are the first k in tasksworkers
Finding the largest k
How do we find the last yes? Trying k = 1, 2, 3 and so on works, but it may run the check once for every possible k. Since all the yes answers come before all the no answers, we can halve the range each time instead. This is binary search, used here on the answer rather than on an array.
Keep two bounds: lohilohimidlomidhimid - 1
The middle must be rounded up, as (lo + hi + 1) / 2lo = 2hi = 3mid = 2lo
Checking one k
Which tasks can the current worker reach with a pill? Workers come weakest first, so each one reaches at least as far as the one before. A task that comes into reach stays in reach for every later worker.
That suits a deque, short for double-ended queue: a list that adds and removes items quickly at both ends. Java's ArrayDeque
For each worker, add the tasks that have just come into reach. If the front task needs no more than the worker's own strength, the worker takes it without a pill. Otherwise nothing in the deque can be done without a pill, so the worker uses one and takes the back task. The check fails when the deque is empty, or when a pill is needed and none are left.
Java Solution
The method maxTaskAssigncanAssign
- First, sort both arrays with
andArrays.sort(tasks) , so the easiest tasks come first and the strongest workers last.Arrays.sort(workers) - Set
to 0 andlo to the smaller of the two lengths. Whilehi is belowlo , computehi and askmid = (lo + hi + 1) / 2 whethercanAssign tasks can be done. A yes setsmid , a no setslo = mid .hi = mid - 1 - Inside
, create an empty deque,canAssign , and an indexreachable , which starts at 0 and marks the first task not yet in reach. A loop then movesnext over the k strongest workers, from positionw to the end, so the weakest of them comes first.workers.length - k - For each worker, whose strength is
, an inner loop addsworker = workers[w] to the back oftasks[next] while it needs no more thanreachable , looking only at the first k tasks.worker + strength - If
is empty, returnreachable . If its front task needs no more thanfalse , remove it withworker . Otherwise returnpollFirst whenfalse is 0, and else use a pill and remove the back task withpills .pollLast - If every worker got a task,
returnscanAssign . When the binary search ends,true returnsmaxTaskAssign .lo
The main
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class MaximumNumberOfTasksYouCanAssign {
public static int maxTaskAssign(int[] tasks, int[] workers, int pills, int strength) {
Arrays.sort(tasks);
Arrays.sort(workers);
int lo = 0;
int hi = Math.min(tasks.length, workers.length);
while (lo < hi) {
int mid = (lo + hi + 1) / 2; // round up, so lo = mid always moves
if (canAssign(mid, tasks, workers, pills, strength)) {
lo = mid; // mid tasks work: try more
} else {
hi = mid - 1; // mid tasks fail: try fewer
}
}
return lo;
}
// Can the k easiest tasks be done by the k strongest workers?
static boolean canAssign(int k, int[] tasks, int[] workers, int pills, int strength) {
Deque<Integer> reachable = new ArrayDeque<>(); // tasks in reach with a pill
int next = 0; // next task not yet in reach
for (int w = workers.length - k; w < workers.length; w++) { // weakest first
int worker = workers[w];
while (next < k && tasks[next] <= worker + strength) {
reachable.addLast(tasks[next++]);
}
if (reachable.isEmpty()) {
return false; // nothing in reach, even with a pill
}
if (reachable.peekFirst() <= worker) {
reachable.pollFirst(); // no pill needed: take the easiest task
} else {
if (pills == 0) {
return false;
}
pills--;
reachable.pollLast(); // with a pill: take the hardest task
}
}
return true;
}
public static void main(String[] args) {
System.out.println(maxTaskAssign(new int[] {3, 2, 1}, new int[] {0, 3, 3}, 1, 1));
System.out.println(maxTaskAssign(new int[] {5, 4}, new int[] {0, 0, 0}, 1, 5));
System.out.println(maxTaskAssign(
new int[] {10, 15, 30}, new int[] {0, 10, 10, 10, 10}, 3, 10));
System.out.println(maxTaskAssign(new int[] {3, 5}, new int[] {2, 4}, 1, 4));
System.out.println(maxTaskAssign(new int[] {5, 4}, new int[] {0, 0, 0}, 0, 5));
System.out.println(maxTaskAssign(new int[] {1, 1, 1, 1}, new int[] {1, 1}, 0, 0));
}
}
Running it prints:
3
1
2
2
0
2
The first three lines, 3, 1 and 2, match the three examples. The fourth line, 2, is the case from the picture: worker 2 takes the pill and the hardest task, 5, and worker 4 does task 3. With pollFirstpollLast
The fifth line is 0. It is Example 2 without its pill, and no worker with strength 0 can do a task that needs 4 or 5. The last line is 2: each of the four tasks needs only 1, but there are only two workers, so hi
Complexity
The work is the two sorts, plus one call of canAssign
- Time: O(n log n + m log m), the cost of sorting the n tasks and the m workers. One check walks k workers and adds and removes each of the k tasks at most once, so it takes O(k) steps. The binary search makes about log₂ min(n, m) checks, which adds O(min(n, m) log min(n, m)), no more than the sorting.
- Space: O(min(n, m)). The deque holds at most k tasks, and k never exceeds min(n, m).
sorts both arrays in place.Arrays.sort
Binary search on the answer, with a quick yes-or-no check for each guess, is also the idea behind