Peak Concurrent Shifts Across Half-Open Time Ranges
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Find the largest number of half-open time ranges that overlap at one instant, handling shared endpoints, empty ranges and large inputs with a sweep over sorted boundaries.
A delivery platform records the periods during which each of its couriers was active. Every period is a half-open range [start, end): the courier counts as active at time start and at every moment up to, but not including, end.
You are given a list shifts, where each element is a two-element list [start, end] of integers. Return the largest number of shifts that are active at the same single moment.
Rules
- A shift is active at time
texactly whenstart <= t < end. - Because the end is excluded, two shifts that touch (one ends at
t, another begins att) are not active together att. - A shift with
start >= endcovers no moment and must be ignored. This includes empty ranges like[5, 5]and malformed ranges where the end is before the start. - Identical shifts are counted separately. Ten copies of
[0, 10]give a peak of10. - If there are no valid shifts (including an empty list), return
0.
Constraints
0 <= len(shifts) <= 10^5(the input may be large, so aim for better than quadratic time).- Each shift is
[start, end]with integer values; values may be negative or very large, and only their relative order matters. - Many shifts may share the same start or end values.
- Return a single integer.
Examples
Example 1
Input: max_concurrent_shifts([[1, 5], [2, 6], [4, 8]])
Output: 3
Example 2
Input: max_concurrent_shifts([[1, 2], [2, 3], [3, 4]])
Output: 1
Example 3
Input: max_concurrent_shifts([[0, 10], [1, 5], [2, 7], [5, 9], [10, 12]])
Output: 3
Hints
Hint 1
Think of each valid shift as two events: the active count goes up by one at its start and down by one at its end.
Hint 2
Group events by timestamp and add up all changes at the same timestamp before looking at the running count. This handles touching ranges without any special tie-break rule.
Hint 3
Sort the distinct timestamps, sweep through them while maintaining the running count, and track the maximum. Skip shifts where start >= end before creating events.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use a sweep line over time.
- Ignore every shift with
start >= end, since it covers no moment. - For each remaining shift, record
+1at its start and-1at its end in a map from timestamp to net change. Shifts that share a timestamp simply add into the same entry. - Sort the distinct timestamps and walk through them in order. At each one, add its net change to a running count of active shifts, then update the best value seen.
Why it is correct
The set of active shifts can only change at a start or end timestamp, so the count is constant between consecutive distinct timestamps. After applying the full net change at timestamp t, the running count equals the number of shifts with start <= t minus the number with end <= t, which is exactly the number of shifts with start <= t < end. That is the number active at t and until the next event. The maximum over all moments is therefore the maximum over these checkpoints.
Applying the whole net change at a timestamp before checking the count is what encodes the half-open rule: a shift ending at t and another starting at t cancel out, so they are never counted together. Starting best at 0 covers the case with no valid shifts.
Complexity
- Time: O(N + K log K), where K <= 2N is the number of distinct timestamps; O(N log N) in the worst case.
- Space: O(K) for the map of net changes.
Reference solution
from collections import defaultdict
from typing import List
def max_concurrent_shifts(shifts: List[List[int]]) -> int:
# Net change in active count at each timestamp.
delta = defaultdict(int)
for start, end in shifts:
if start < end:
delta[start] += 1
delta[end] -= 1
active = 0
best = 0
for t in sorted(delta):
active += delta[t]
if active > best:
best = active
return best