Peak Overlap of Half-Open Time Ranges
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Compute the largest number of simultaneously running half-open time ranges using a sweep over sorted start and end events, with correct handling of ties at boundaries.
Peak Concurrent Ranges
You are given a collection of time ranges. Each range is a pair [begin, finish] and covers every moment t with begin <= t < finish. In other words, the range includes its begin moment but does not include its finish moment.
Determine the greatest number of ranges that cover one single moment in time.
Because finish is excluded, a range that finishes at time t does not overlap with a range that begins at time t.
Input
loans: a list of[begin, finish]pairs of integers, withbegin < finish.
Output
Return one integer: the maximum number of ranges that are simultaneously active at any moment.
Constraints
1 <= len(loans) <= 10^50 <= begin < finish <= 10^9- Several ranges may share the same begin time, the same finish time, or both.
- A single range is a valid input and gives an answer of
1. - Time values are too large to allocate an array indexed by time.
Examples
Example 1
Input: max_active_loans([[1, 5], [2, 6], [5, 8]])
Output: 2
Example 2
Input: max_active_loans([[1, 10], [2, 3], [4, 5]])
Output: 2
Hints
Hint 1
The number of active ranges can only change at a begin time or a finish time, so you only need to examine those moments.
Hint 2
Break each range into two events: one that adds 1 at its begin time and one that subtracts 1 at its finish time, then process the events in time order.
Hint 3
When a begin and a finish share the same time, handle the finish first, because the finishing range is no longer active at that moment. Sorting (time, delta) tuples with delta -1 for finishes does this automatically.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The number of active ranges changes only at begin and finish times. So we turn every range into two events, (begin, +1) and (finish, -1), sort all events, and scan them while maintaining a running count and the highest value it reaches.
The tie-break matters. Ranges are half-open, so a range finishing at time t is not active at t, and it must not be counted together with a range beginning at t. When two events share a time, the finish (-1) must be processed before the begin (+1). Sorting (time, delta) tuples does this naturally, since -1 < 1.
Why it is correct
- The count is constant between consecutive distinct event times, so the maximum occurs right after some event.
- At a given time
t, processing all finishes first removes every range that no longer containst, and then processing all begins adds every range that does containt. After the last event att, the count equals the exact number of ranges active att. - Intermediate values within a timestamp are never larger than both the count just before
tand the count just aftert, because decrements come before increments. They therefore never inflate the peak.
A time-indexed difference array would need up to 10^9 slots, so sorting the 2n events is the right choice.
Complexity
- Time: O(n log n), dominated by sorting 2n events.
- Space: O(n) for the event list.
Reference solution
from typing import List
def max_active_loans(loans: List[List[int]]) -> int:
events = []
for begin, finish in loans:
events.append((begin, 1))
events.append((finish, -1))
events.sort()
active = 0
peak = 0
for _, delta in events:
active += delta
if active > peak:
peak = active
return peak