KatInterview

Minimum Resources Required for Scheduled Jobs

Greedy & IntervalsAuto-graded · 18 test cases
MediumAuto-gradedGreedy & Intervalsgreedy-intervalsheaps-sortingarrays

Last updated

Determine the minimum number of resources required to run a schedule of tasks given start and end times with boundary reuse allowed.

You are given a list of tasks that must be executed. Each task is represented as a pair of integers [start, end], where start is the time the task begins and end is the time it finishes.

Each resource can work on at most one task at any given instant. Tasks occupy the half-open interval [start, end). This means that if a task finishes at time t, the resource assigned to it becomes immediately available at time t to begin another task starting at t.

Calculate the minimum number of resources required to complete all scheduled tasks.

Constraints

  • 0 <= len(tasks) <= 200,000
  • For each task [start, end], 0 <= start < end <= 1,000,000,000
  • The tasks in the input list may appear in any order.
  • Duplicate task intervals are permitted and represent distinct tasks that each require a resource.
  • If no tasks are provided, return 0.

Examples

Example 1

Input: minimum_buses([[0, 10], [5, 8], [8, 12], [10, 15]])

Output: 2

Example 2

Input: minimum_buses([[4, 7], [1, 4], [7, 9]])

Output: 1

Hints

Hint 1

Consider what determines the minimum number of resources: how many tasks run simultaneously at the peak concurrency point?

Hint 2

If a task ends at time t and another task starts at time t, can they share the same resource?

Hint 3

Try sorting the start times and end times separately, then using a sweep-line technique with two pointers to count overlapping tasks.