Skip to content

2895. Minimum Processing Time 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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]))