KatInterview

Maximum Concurrent Reservations

Greedy & IntervalsAuto-graded · 25 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-linesorting

Last 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 t where start <= t < end.
    • A resource freed at time t becomes immediately available for a new reservation starting at time t.

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.