Maximum Concurrent Reservations
Greedy & IntervalsAuto-graded · 25 test casesLast updated
Calculate the peak number of overlapping time intervals to determine minimum resource capacity.
You're building a resource allocation system that processes reservation requests. Each reservation occupies a resource during a specific time window. Your task is to determine the minimum number of resources required to fulfill all reservations without conflicts.
Write a function that calculates the maximum number of simultaneously active reservations at any point in time.
Input
reservations: A list where each element[start, end]represents a reservation occupying a resource during the half-open time interval[start, end).- The resource is occupied at every integer time
twherestart <= t < end. - A resource freed at time
tbecomes immediately available for a new reservation starting at timet.
- The resource is occupied at every integer time
Output
Return an integer representing the peak concurrent reservation count—the maximum number of reservations active at the same moment.
Rules
- Each reservation requires exactly one resource, even if multiple reservations have identical time windows.
- Resources are fungible: any resource can fulfill any reservation.
- When no reservations exist, return
0. - Time values are integers representing arbitrary time units.
Constraints
0 <= len(reservations) <= 10^5- For each reservation
[start, end]:0 <= start < end <= 10^9 - Input order is arbitrary
Examples
Example 1
Input: max_concurrent_reservations([[1, 4], [2, 5], [7, 9], [3, 6]])
Output: 3
Example 2
Input: max_concurrent_reservations([[1, 3], [3, 5], [5, 7]])
Output: 1
Example 3
Input: max_concurrent_reservations([[5, 10], [5, 10], [9, 12], [10, 11]])
Output: 3
Hints
Hint 1
Think about what happens at the boundaries: when do reservations begin and when do they end?
Hint 2
If you track all boundary events in sorted order, you can compute how many reservations are active without checking every time value.
Hint 3
Pay careful attention to the half-open interval rule: a resource released at time t can immediately serve a new reservation at time t.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that the number of active reservations changes only at reservation boundaries—when one starts or ends. We don't need to examine every possible time value.
Transform each reservation [s, e) into two boundary events:
(s, +1): a resource becomes occupied at times(e, -1): a resource becomes free at timee
Sort all events chronologically and sweep through them while maintaining a running count of active reservations. Track the maximum count encountered.
The critical detail is handling simultaneous events correctly. Since intervals are half-open [start, end), a resource freed at time t can immediately serve a reservation starting at t. This means release events (-1) must be processed before acquisition events (+1) at the same timestamp.
Python's tuple sorting provides this automatically: when sorting (time, delta) pairs, equal times are ordered by the second element, and -1 < +1.
Why It Works
After processing all events up to time t, the running count equals the number of reservations with start <= t minus those with end <= t. This is precisely the count of reservations satisfying start <= t < end—the active reservations at time t.
The maximum overlap directly determines minimum capacity: if k reservations are simultaneously active, you need at least k resources. Conversely, you can serve any set of reservations with exactly k resources by reusing freed resources greedily.
Complexity
- Time:
O(n log n)wherenis the number of reservations. Creating2nevents is linear; sorting dominates. - Space:
O(n)for the event list.
With up to 100,000 reservations, this produces roughly 200,000 events, which sorts efficiently.
def max_concurrent_reservations(reservations: list[list[int]]) -> int:
events = []
for s, e in reservations:
events.append((s, 1))
events.append((e, -1))
events.sort()
cur = best = 0
for _, d in events:
cur += d
if cur > best:
best = cur
return best