Advertisement

Minimum Processing Time - LeetCode 2895 Solution

Minimum Processing Time - Complete Solution Guide

Minimum Processing Time is LeetCode problem 2895, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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,

Detailed Explanation

The problem requires finding the minimum time needed to complete a set of tasks given a number of processors with 4 cores each. Each task must be assigned to a unique core, and each core can only be used once. We are given `processorTime`, representing the time each processor becomes available, and `tasks`, representing the time it takes to complete each task. The goal is to assign tasks to processors such that the maximum completion time among all processors is minimized. The input `tasks` array will always have a length that is four times the length of the `processorTime` array.

Solution Approach

The solution uses a greedy approach. It sorts the `processorTime` in ascending order and the `tasks` in descending order. Then, it iterates through the processors and assigns the four longest remaining tasks to each processor. For each processor, it calculates the completion time as the processor's availability time plus the time of its longest task. The maximum of these completion times across all processors is the minimum time needed to complete all tasks.

Step-by-Step Algorithm

  1. Step 1: Sort the `processorTime` array in ascending order.
  2. Step 2: Sort the `tasks` array in descending order.
  3. Step 3: Iterate through the sorted `processorTime` array from index 0 to n-1, where n is the number of processors.
  4. Step 4: In each iteration, calculate the completion time for the current processor. This is the sum of the processor's availability time and the task at index `i * 4` in the sorted `tasks` array. Since `tasks` is sorted descending, index `i*4` gives us the largest task assigned to the i-th processor.
  5. Step 5: Update the `max_time` with the maximum completion time encountered so far.
  6. Step 6: Return the `max_time` after iterating through all processors.

Key Insights

  • Insight 1: Sorting `processorTime` in ascending order and `tasks` in descending order allows us to greedily assign the longest tasks to the earliest available processors.
  • Insight 2: The maximum time for each processor is determined by the processor's availability time plus the longest task assigned to it.
  • Insight 3: The overall minimum processing time is the maximum of all processor completion times.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy, Sorting.

Companies

Asked at: Akuna Capital, Nutanix.