Greedy · Medium
A scheduling tool selects requests for one resource. Each request occupies a time window and has equal value, so the objective is to accept as many as possible. The caller explicitly chooses whether a shared endpoint counts as an overlap; reversed imported endpoint pairs must be normalized.
Return the largest number of pairwise non-overlapping retained requests under the supplied endpoint rule.
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 an integer count, not total duration. Empty input or only empty half-open windows returns zero.
- With closed windows a new request must start strictly after the previous end; with half-open windows it may start at that end. Duplicate closed point requests conflict.
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,
10
],
[
1,
2
],
[
3,
4
]
]
}Returns
2Example 2
{
"closed": false,
"intervals": [
[
1,
2
],
[
2,
3
]
]
}Returns
2Required complexity
O(n log(n + 1)) time and O(n) auxiliary space