Minimum Resources Required for Scheduled Jobs
Greedy & IntervalsAuto-graded · 18 test casesLast 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks for the minimum fleet size (or number of resources) needed to execute a collection of tasks. The minimum number of resources required equals the maximum number of tasks running concurrently at any moment.
Since a task occupies the half-open interval [start, end), a task finishing at time t frees up a resource exactly when another task starting at time t begins. Therefore, at time t, an ending task should be processed before a starting task so that the freed resource can be reused immediately.
We can solve this problem efficiently using a two-pointer / sweep-line technique on sorted start and end times:
- Extract and sort all
starttimes independently into one array (starts). - Extract and sort all
endtimes independently into another array (ends). - Iterate through both arrays using two pointers,
i(forstarts) andj(forends):- If
starts[i] < ends[j], a task starts before the earliest active task ends. Increment the active task count and advancei. Update the peak concurrent count. - If
starts[i] >= ends[j], an active task has finished (or finishes at the exact same moment). Decrement the active task count and advancej.
- If
- Stop when all task starts have been processed (
i == n).
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$ where $N$ is the number of tasks, due to sorting the start and end arrays. The two-pointer sweep takes $\mathcal{O}(N)$ time.
- Space Complexity: $\mathcal{O}(N)$ auxiliary space to store the sorted start and end arrays.
Python Reference Solution
from typing import List
def minimum_buses(trips: List[List[int]]) -> int:
if not trips:
return 0
starts = sorted(start for start, _ in trips)
ends = sorted(end for _, end in trips)
n = len(trips)
i = 0
j = 0
active = 0
max_active = 0
while i < n:
if starts[i] < ends[j]:
active += 1
if active > max_active:
max_active = active
i += 1
else:
active -= 1
j += 1
return max_active