2895. Minimum Processing Time
Asked at JPMorgan
Difficulty
Medium
Time
O(n log n + m log m)
Space
O(1) extra
Pattern
Array
Companies
1
Problem
You have n processors. Each processor has 4 cores, and every core runs exactly one task. So the number of tasks is always 4 * n.
Each processor becomes free at a given time. You get processorTime, where processorTime[i] is the moment processor i is ready to start. You also get tasks, where tasks[j] is how long task j takes to run. A processor can only start after it is available, and it runs its four tasks on its four cores at the same time.
A processor finishes when its slowest task is done, so its finish time is availableTime + longestTask. Return the smallest possible time by which all tasks are finished, choosing which tasks go to which processor.
Two processors, four cores each
Constraints
- ●
1 <= n == processorTime.length <= 25000 - ●
1 <= tasks.length <= 10^5 - ●
0 <= processorTime[i] <= 10^9 - ●
1 <= tasks[i] <= 10^9 - ●
tasks.length == 4 * n
Examples
processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]16processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]23Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| JPMorgan | Medium | View all JPMorgan questions → |
How to Think About It
A processor finishes at availableTime + (longest task it runs). Only the longest of its four tasks matters.
Key insight: pair early processors with long tasks. Sort processors ascending and tasks descending, then give processor i the tasks at positions 4i..4i+3.
The finish time of processor i is processorTime[i] + tasks[4i] (its longest task). The answer is the maximum over processors.
Exchange argument: if an early processor had a shorter longest-task than a later one, swapping those tasks never increases the maximum.
Walkthrough: processors [8,10], tasks [2,2,3,1,8,7,4,5]. Tasks sorted desc [8,7,5,4,3,2,2,1]. Processor 8 takes 8,7,5,4 -> 16; processor 10 takes 3,2,2,1 -> 13. Answer 16.
Optimal Approach
Step 1: Sort processorTime ascending and tasks descending.
Step 2: res = max(processorTime[i] + tasks[4 * i] for i).
Step 3: Return res.
Time: O(n log n). Space: O(1) extra.
Sort processors up, tasks down, then group by four
What Trips People Up in Real Interviews
Giving the longest tasks to the latest processor — exactly backwards.
Summing task durations per processor. Cores run in parallel, so only the longest task on each processor counts.
Sorting both arrays in the same direction.
Not explaining the exchange argument.
Solution Code
O(n log n + m log m) timedef minProcessingTime(processorTime, tasks):
processorTime.sort()
tasks.sort(reverse=True)
return max(p + tasks[4 * i] for i, p in enumerate(processorTime))Frequently Asked Questions
What is the Minimum Processing Time problem?
Minimum Processing Time gives you processors (each with 4 cores and an availability time) and exactly `4n` tasks. Each core runs one task. Minimize the time when all tasks finish. The greedy: the earliest-available processor should take the longest tasks.
How do you solve Minimum Processing Time?
The optimal approach runs in O(n log n + m log m) time and O(1) extra space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Minimum Processing Time?
Minimum Processing Time is asked at JPMorgan. It is a medium difficulty problem.
What are common mistakes on Minimum Processing Time?
- Giving the longest tasks to the latest processor — exactly backwards.
- Summing task durations per processor. Cores run in parallel, so only the longest task on each processor counts.
- Sorting both arrays in the same direction.
- Not explaining the exchange argument.