Problem Description
You are given an array processorTime where each element is the time at which a processor becomes available. Each processor has 4 cores and there are exactly 4×n tasks given in the array tasks (each task taking some time to execute). Each task must be assigned to a unique core and a processor can only process 4 tasks (one per core). When a processor p starts executing a task with time t, the finish time is p + t. The goal is to distribute the tasks among the processors so that all tasks are completed and the overall finish time (i.e. the maximum finish time among all processors) is minimized.
Key Insights
- There are exactly 4 tasks per processor and tasks can be arbitrarily assigned to cores.
- The finish time for a processor is determined by its available time plus the maximum time among the 4 tasks it processes.
- To minimize the overall maximum finish time, larger tasks should ideally be paired with processors that are available earlier.
- A binary search on the candidate “maximum finish time” can be used to decide if all tasks can be assigned under that threshold.
- A greedy check works by assigning tasks in descending order (largest first) to processors in ascending order (smallest available time first).
Space and Time Complexity
Time Complexity: O(totalTasks * log(max(processorTime)+max(tasks))). In worst-case, total tasks = 4*n and n ≤ 25,000 with log factor coming from binary search. Space Complexity: O(n + totalTasks) for sorting the processor and tasks arrays.
Solution
The idea is to use binary search to find the smallest maximum finish time M such that all tasks can be assigned properly. For a candidate M, we check each processor (sorted in increasing order of availability) and attempt to assign it 4 tasks from the list of all tasks (sorted in descending order so that the largest tasks get the “best” – smallest – processor). For each processor with available time p, it can only process a task t if p + t ≤ M, i.e. t ≤ M - p. If it is not possible to assign 4 tasks for each processor under M, then M is too small. This greedy assignment check is performed for every candidate M in the binary search. Finally, the binary search converges on the minimum M for which an assignment exists.