Minimum Processing Time - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: processorTime = [8,10], tasks = [2,2,3,1,8,7,4,5]
Output: 26
💡 Note: Sort processors: [8,10]. Sort tasks: [8,7,5,4,3,2,2,1]. Assign tasks[0,2,4,6] = [8,5,3,2] to processor 0 (completion: 8+18=26) and tasks[1,3,5,7] = [7,4,2,1] to processor 1 (completion: 10+14=24). Maximum completion time is 26.
Example 2 — Equal Processors
$ Input: processorTime = [10,20], tasks = [2,3,1,2,5,8,4,3]
Output: 31
💡 Note: Sort processors: [10,20]. Sort tasks: [8,5,4,3,3,2,2,1]. Assign tasks[0,2,4,6] = [8,4,3,2] to processor 0 (completion: 10+17=27) and tasks[1,3,5,7] = [5,3,2,1] to processor 1 (completion: 20+11=31). Maximum completion time is 31.
Example 3 — Single Processor
$ Input: processorTime = [0], tasks = [1,2,3,4]
Output: 10
💡 Note: One processor gets all 4 tasks: 0 + (1+2+3+4) = 10

Constraints

  • 1 ≤ n ≤ 104 (where n is number of processors)
  • tasks.length == 4 * n
  • 1 ≤ processorTime[i] ≤ 109
  • 1 ≤ tasks[i] ≤ 109

Visualization

Tap to expand
Minimum Processing Time INPUT Processor Times: 8 P0 10 P1 Each processor has 4 cores Tasks (8 total): 2 2 3 1 8 7 4 5 processorTime = [8, 10] tasks = [2,2,3,1,8,7,4,5] Goal: Minimize max completion time ALGORITHM STEPS 1 Sort Processors (ASC) [8, 10] (already sorted) 2 Sort Tasks (DESC) [8,7,5,4,3,2,2,1] 3 Assign to Processors 4 largest tasks to P0 P0 (starts@8): Core1: 8 --> 8+8=16 Core2: 7 --> 8+7=15 Core3: 5 --> 8+5=13 P1 (starts@10): Core1: 4 --> 10+4=14 Core2: 3 --> 10+3=13 Core3: 2 --> 10+2=12 4 Find Maximum max(16,15,13,14,13,12, 11,11) = 16 P0-Core4: 4 --> 8+4=12 P1-Core4: 1 --> 10+1=11 FINAL RESULT Timeline View: 0 8 10 16 P0: Task 8 (16) Task 7 (15) Task 5 Task 4 P1: Task 4 Task 3 T2 1 MAX Output: 16 Minimum completion time achieved! [OK] Key Insight: Greedy Strategy: Assign longest tasks to earliest-available processors. Sort processors by start time (ascending) and tasks by duration (descending). This ensures the longest task runs on the processor that starts earliest, minimizing the maximum completion time across all cores. TutorialsPoint - Minimum Processing Time | Greedy - Sort and Assign Optimally
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen