Peak Concurrent Tasks: Minimum Workers Needed
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Tests interval overlap detection using sweep-line or heap techniques to find the maximum number of simultaneously active tasks.
Problem
You manage a pipeline of tasks, each defined by a start time and a finish time. A worker can only handle one task at a time, but as soon as a task finishes at time T, that same worker is immediately available to pick up a new task starting at time T.
Given a list of tasks where tasks[i] = [start, finish] describes when task i begins and ends (start < finish), return the minimum number of workers required so that every task runs without delay.
Constraints
1 <= len(tasks) <= 10^5tasks[i].length == 20 <= tasks[i][0] < tasks[i][1] <= 10^9
Notes
- A worker finishing at time
Tis available for a task starting at timeT— back-to-back assignments do not conflict. - Tasks may be given in any order.
- Fully nested tasks (e.g., one task entirely contained within another's window) each require their own worker.
Examples
Example 1
Input: min_workers_needed([[0, 30], [5, 10], [15, 20]])
Output: 2
Example 2
Input: min_workers_needed([[7, 10], [2, 4]])
Output: 1
Hints
Hint 1
The minimum number of workers equals the maximum number of tasks that are simultaneously in progress at any single moment in time. Try to find that peak overlap count.
Hint 2
Sort the start times and end times into two separate arrays. Use two pointers to walk through them in tandem: each unmatched start increments a counter, and each end that arrives before or at the next start decrements it.
Hint 3
If you prefer a heap approach, sort tasks by start time and maintain a min-heap of finish times — one entry per busy worker. Before assigning the next task, check whether the earliest-finishing worker is already free (heap top <= current start). The key tie-break: use <= not <, because a worker finishing at T can start a new task at T.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that the minimum number of workers equals the maximum number of tasks that overlap at any single point in time. If k tasks are all in progress simultaneously, you need at least k workers — no single worker can cover two of them. Conversely, a greedy assignment never opens a new worker unless every existing one is still busy, so the count never exceeds the true overlap peak. The two bounds meet, proving the approach optimal.
Sweep-line with two pointers
Separate the task list into two sorted arrays: one of all start times, one of all finish times. Then walk through the starts one by one with a pointer, and advance the finish pointer whenever the earliest pending finish is <= the current start:
- If the current start arrives before the earliest finish, one more worker is needed — increment the active count and advance the start pointer.
- Otherwise, the earliest finish is
<=the current start, meaning one worker is already free. Decrement active and advance the finish pointer — effectively reassigning that worker before counting the next start.
Track the highest value active reaches; that is the answer.
Why <= and not <? A worker finishing at exactly time T is free to take a task starting at T. Using < would incorrectly treat that as an overlap and open an extra worker unnecessarily.
Why the pairing doesn't matter
After sorting, a start and its original finish may no longer be paired together. That is fine — only the count of concurrently active tasks matters, not which specific task pairs with which worker. The sorted arrays correctly track how many tasks are alive at each event point.
Complexity
| Time | Space | |
|---|---|---|
| Sorting | O(N log N) | O(N) for the two sorted arrays |
| Two-pointer scan | O(N) | O(1) extra |
| Total | O(N log N) | O(N) |
With N up to 10^5, this runs comfortably within any reasonable time limit.
Edge cases
- Shared boundary
[[1,5],[5,10]]→1. The finish5 <= start 5triggers a free-worker decrement before the second task increments the count. - Fully nested
[[1,10],[2,5]]→2. The inner task starts at2, but the outer task's finish10has not arrived yet, so active reaches2. - Identical starts
[[1,4],[1,4],[1,4]]→3. All three starts come before any finish. - Single task →
1. The loop runs once and returns1.
Reference solution
from typing import List
def min_workers_needed(tasks: List[List[int]]) -> int:
starts = sorted(start for start, _ in tasks)
ends = sorted(finish for _, finish in tasks)
start_idx = 0
end_idx = 0
active = 0
peak = 0
while start_idx < len(starts):
if starts[start_idx] < ends[end_idx]:
# A new task begins before the earliest-ending active task finishes.
# We need one more worker.
active += 1
peak = max(peak, active)
start_idx += 1
else:
# The earliest finish is <= this start, so one worker becomes free
# before (or exactly when) the next task begins.
active -= 1
end_idx += 1
return peak