#2895

Minimum Processing Time

Medium
ArrayGreedySortingGreedySorting
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n log n)
Space
O(1)
O(n)
💡

Intuition

Time O(n log n)Space O(n)

The optimal solution leverages sorting and greedy assignment. By sorting both the processor times and task durations, we can efficiently assign tasks to minimize the overall completion time.

⚙️

Algorithm

4 steps
  1. 1Step 1: Sort the processorTime array in ascending order.
  2. 2Step 2: Sort the tasks array in descending order.
  3. 3Step 3: Assign the longest tasks to the processors with the earliest available times, ensuring each processor gets 4 tasks.
  4. 4Step 4: Calculate the finish time for each processor and return the maximum finish time.
solution.py9 lines
1def minProcessingTime(processorTime, tasks):
2    processorTime.sort()
3    tasks.sort(reverse=True)
4    n = len(processorTime)
5    finish_times = [0] * n
6    for i in range(len(tasks)):
7        processor_index = i // 4
8        finish_times[processor_index] = max(finish_times[processor_index], processorTime[processor_index] + tasks[i])
9    return max(finish_times)

Complexity note: The time complexity is dominated by the sorting steps, which take O(n log n). The space complexity is O(n) for storing finish times.

  • 1Assigning longer tasks to processors that become available earlier minimizes overall completion time.
  • 2Sorting both the processors and tasks helps in efficiently pairing them.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.