Greedy · Medium
A booking service needs enough identical rooms for all requests at once. It must measure peak simultaneous occupancy rather than choose which requests to keep. When one reservation ends as another starts, the caller’s endpoint rule determines whether they share a room or briefly overlap.
Return the maximum number of retained reservations active at the same real-valued time.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ intervals: [integer, integer][], closed: boolean }
Fields
- Each interval is an unordered endpoint pair [a,b]. Normalize it to start=min(a,b), end=max(a,b); input order is arbitrary.
- closed=true means [start,end], including both endpoints. closed=false means [start,end), excluding end. Ignore zero-length half-open ranges entirely; a zero-length closed range is one point.
Output
Return value
- Return a non-negative integer room count. Empty input gives zero.
- Duplicate requests count separately. Closed point reservations occupy their single instant; empty half-open reservations occupy nothing.
Constraints
- 0 <= intervals.length <= 100000. Endpoints are integers from -1000000000 to 1000000000.
- closed is always a boolean. Do not mutate the input or its nested pairs. Duplicate intervals remain separate requests unless the objective merges coverage.
Examples
Example 1
{
"closed": true,
"intervals": [
[
0,
5
],
[
1,
2
],
[
2,
3
]
]
}Returns
3Example 2
{
"closed": false,
"intervals": [
[
0,
5
],
[
1,
2
],
[
2,
3
]
]
}Returns
2Required complexity
O(n log(n + 1)) time and O(n) auxiliary space