Problem · Intervals

Task Scheduling

Learn this problem
MediumJPMorgan ChaseINTERNOA

Problem statement

Given a set of n tasks, the task at index i, where 0 ≤ i < n, runs from time start[i] through end[i]. Find the minimum number of machines required to complete the tasks. A task can be scheduled on exactly one machine, and one machine can run only one task at a time.

Function

getMinMachines(start: int[], end: int[]) → int

Examples

Example 1

start = [1, 8, 3, 9, 6]end = [7, 9, 6, 14, 7]return = 3
Consider the following task schedule. Times in parentheses are the inclusive start and end times for each job. - Machine 1: [(1, 7), (8, 9)] - Machine 2: [(3, 6), (9, 14)] - Machine 3: [(6, 7)] Here, the number of machines required is 3.

Example 2

start = [2, 1, 5, 5, 8]end = [5, 3, 8, 6, 12]return = 3
  • Machine 1: [(2, 5), (8, 12)]
  • Machine 2: [(1, 3), (5, 8)]
  • Machine 3: [(5, 6)]

At time 5, three tasks are active because task endpoints are inclusive, so three machines are necessary and sufficient.

Example 3

start = [2, 2, 2, 2]end = [5, 5, 5, 5]return = 4
- Machine 1: [(2, 5)] - Machine 2: [(2, 5)] - Machine 3: [(2, 5)] - Machine 4: [(2, 5)]

Constraints

  • 1 ≤ n ≤ 2 * 105
  • 1 ≤ start[i] ≤ end[i] ≤ 109

More JPMorgan Chase problems

drafts saved locally
public int getMinMachines(int[] start, int[] end) {
  // write your code here
}
start[1, 8, 3, 9, 6]
end[7, 9, 6, 14, 7]
expected3
checking account