You have a certain number of processors, each having 4 cores. The number of tasks to be executed is four times the number of processors. Each task must be assigned to a unique core, and each core can only be used once.
You are given an array processorTime representing the time each processor becomes available and an array tasks representing how long each task takes to complete. Return the minimum time needed to complete all tasks.
Example 1:
Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
Output: 16
Explanation:
Assign the tasks at indices 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indices 0, 1, 2, 3 to the second processor which becomes available at time = 10.
The time taken by the first processor to finish the execution of all tasks is max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
The time taken by the second processor to finish the execution of all tasks is max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
Example 2:
Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
Output: 23
Explanation:
Assign the tasks at indices 1, 4, 5, 6 to the first processor and the others to the second processor.
The time taken by the first processor to finish the execution of all tasks is max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18.
The time taken by the second processor to finish the execution of all tasks is max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23.
Constraints:
1 <= n == processorTime.length <= 250001 <= tasks.length <= 1050 <= processorTime[i] <= 1091 <= tasks[i] <= 109tasks.length == 4 * nProblem Overview: You are given two arrays: processorTime and tasks. Each processor must handle exactly four tasks. A processor can only start processing after its own setup time, and the total completion time for that processor depends on the largest task assigned to it. The goal is to distribute tasks so the overall finishing time across all processors is minimized.
Approach 1: Sorting with Task Distribution Optimization (O(n log n) time, O(1) extra space)
The key observation is that each processor receives exactly four tasks, and the processor’s finish time is dominated by the largest task assigned to it. To minimize the maximum completion time, assign larger tasks to processors with smaller setup times. Start by sorting processorTime in ascending order and tasks in descending order. Then distribute tasks in groups of four: processor i receives tasks starting from index 4 * i. Because the tasks array is sorted descending, the first element in each group represents the largest task for that processor. Compute processorTime[i] + tasks[4*i] and track the maximum across all processors. Sorting dominates the runtime, giving O(n log n) time with O(1) extra space if sorting in place. This approach relies heavily on sorting and a simple greedy assignment strategy.
Approach 2: Greedy Assignment with Two-Pointer Technique (O(n log n) time, O(1) space)
This version expresses the same greedy logic using two pointers after sorting. Sort processorTime in ascending order and tasks in descending order. Use one pointer i to iterate through processors and another pointer j to track the current largest unassigned task. For each processor, assign the next four tasks starting from j. Only the first task in that group determines the processor’s completion time because it is the largest among the four. Update the answer with processorTime[i] + tasks[j] and move j += 4. The two-pointer pattern makes the distribution explicit and keeps the implementation clean. Complexity remains O(n log n) due to sorting, with constant additional memory. The arrays themselves are the main data structures, making this a classic array greedy scheduling problem.
Recommended for interviews: The sorting-based greedy strategy is what interviewers expect. It demonstrates that you recognized the constraint of exactly four tasks per processor and reduced the problem to pairing smallest processor setup times with largest task groups. Brute-force simulation would be inefficient and unnecessary, while the greedy sorting approach shows strong algorithmic intuition and clean complexity analysis.
In this approach, the key idea is to sort the task times and the processor availability times to efficiently allocate tasks such that the maximum processing time is minimized.
Sort the tasks in descending order and processors by their availability. Assign the largest unassigned tasks to the most available processors iteratively.
The C solution involves sorting the tasks and processorTime arrays. We process tasks in groups of four assigned to each processor. The largest possible processing end time is computed for each group, which helps determine the minimal time to complete all tasks.
Time Complexity: O(m log m + n log n) where m = 4n is the number of tasks and n is the number of processors.
Space Complexity: O(1) as we use constant extra space.
This approach uses a two-pointer technique. With one pointer on the processors' array and one on the task array, use a greedy approach to find the minimum completion time by balancing tasks based on processor availability without explicit sorting.
This C solution uses the two-pointer technique after sorting the processorTime and tasks arrays. It aims to complete tasks from the processing cores with a greedy approach.
Time Complexity: O(m log m + n log n) because of the sorting.
Space Complexity: O(1).
To minimize the time required to process all tasks, the four tasks with the longest processing time should be assigned to the processors that become idle earliest.
Therefore, we sort the processors by their idle time and sort the tasks by their processing time. Then, we assign the four tasks with the longest processing time to the processor that becomes idle earliest, and calculate the maximum end time.
The time complexity is O(n times log n), and the space complexity is O(log n). Here, n is the number of tasks.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Sorting with Task Distribution Optimization | Time Complexity: |
| Greedy Assignment with Two-Pointer Technique | Time Complexity: |
| Greedy + Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting with Task Distribution Optimization | O(n log n) | O(1) | Best general solution when tasks must be grouped per processor |
| Greedy Assignment with Two-Pointer Technique | O(n log n) | O(1) | Useful when implementing explicit task grouping after sorting |
2895. Minimum Processing Time 🔥 || Greedy + Sorting 🔥 || (C++,JAVA,Python)🔥 • Ayush Rao • 1,320 views views
Watch 6 more video solutions →Practice Minimum Processing Time with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor