FastPrepFind Minimum Time

Find Minimum Time

BlackRock logoBlackRock● MediumFULLTIMEOA

Problem statement

There are n processes to run. Process i has size processSize[i]. There are m processors, and processor j has capacity capacity[j].

A processor can run a process only if the process size is at most its capacity. Running one process takes 1 second.

A processor may run several processes one after another, but after it finishes a process it must rest for 1 second before it starts the next one. Different processors can work at the same time.

Return the minimum number of seconds needed to run every process, or -1 if some process cannot run on any processor.

Function

findMinimumTime(processSize: int[], capacity: int[]) → int

Examples

Example 1

processSize = [2, 5, 3]capacity = [6, 2, 4]return = 1

Give the process of size 5 to the processor with capacity 6, the process of size 2 to the processor with capacity 2, and the process of size 3 to the processor with capacity 4. All three processors work at the same time, so every process is done after 1 second.

Example 2

processSize = [1, 2, 3, 4, 6]capacity = [4, 7, 4]return = 3

There are five processes and only three processors, so some processor must run at least two processes. That processor runs one process, rests for 1 second, and then runs the other, so it needs 3 seconds.

One plan that finishes in 3 seconds: the first processor (capacity 4) runs sizes 2 and 3, the second processor (capacity 7) runs sizes 1 and 6, and the third processor (capacity 4) runs size 4.

Example 3

processSize = [2, 5, 8]capacity = [6, 7, 4]return = -1

The largest capacity is 7, so no processor can run the process of size 8. The answer is -1.

Example 4

processSize = [3, 3, 3]capacity = [5]return = 5

The only processor must run all three processes. It runs them in seconds 1, 3, and 5 and rests in seconds 2 and 4, so the answer is 5.

Constraints

  • 1 <= processSize.length <= 10^5
  • 1 <= capacity.length <= 10^5
  • 1 <= processSize[i] <= 10^9
  • 1 <= capacity[j] <= 10^9

More BlackRock problems

See BlackRock hiring insights
Saved
public int findMinimumTime(int[] processSize, int[] capacity) {
  // Write your code here.
}
Checking account…