2895. Minimum Processing Time LeetCode Solution
In this guide, you will get 2895. Minimum Processing Time LeetCode Solution with the best time and space complexity. The solution to Minimum Processing Time problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Processing Time solution in C++
- Minimum Processing Time solution in Java
- Minimum Processing Time solution in Python
- Additional Resources
Problem Statement of Minimum Processing Time
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 <= 25000
1 <= tasks.length <= 105
0 <= processorTime[i] <= 109
1 <= tasks[i] <= 109
tasks.length == 4 * n
Complexity Analysis
- Time Complexity:
- Space Complexity:
2895. Minimum Processing Time LeetCode Solution in C++
class Solution {
public:
int minProcessingTime(vector<int>& processorTime, vector<int>& tasks) {
int ans = 0;
ranges::sort(processorTime);
ranges::sort(tasks, greater<>());
// It's optimal to run each 4 longer tasks with a smaller processor time.
// Therefore, for each processor, take the maximum of the sum of the
// processor time and the largest assigned tasks[i].
for (int i = 0; i < processorTime.size(); ++i)
ans = max(ans, processorTime[i] + tasks[i * 4]);
return ans;
}
};
/* code provided by PROGIEZ */
2895. Minimum Processing Time LeetCode Solution in Java
class Solution {
public int minProcessingTime(List<Integer> processorTime, List<Integer> tasks) {
int ans = 0;
Collections.sort(processorTime);
Collections.sort(tasks, Collections.reverseOrder());
// It's optimal to run each 4 longer tasks with a smaller processor time.
// Therefore, for each processor, take the maximum of the sum of the
// processor time and the largest assigned tasks[i].
for (int i = 0; i < processorTime.size(); ++i)
ans = Math.max(ans, processorTime.get(i) + tasks.get(i * 4));
return ans;
}
}
// code provided by PROGIEZ
2895. Minimum Processing Time LeetCode Solution in Python
class Solution:
def minProcessingTime(
self,
processorTime: list[int],
tasks: list[int],
) -> int:
return max(time + task
for (time, task) in zip(
sorted(processorTime),
sorted(tasks)[:: -4]))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.