Community Watch Relay Windows
Fewest selected ranges covering each requested span
A social network coordinates volunteer watch sessions. Each session is available throughout a closed time interval, and volunteers can hand off responsibility at a shared endpoint.
You receive windows, where windows[i] = [start, end] describes one available session. You also receive requests, where requests[j] = [left, right] asks for uninterrupted coverage of the entire closed interval [left, right].
For each request, return the minimum number of available sessions whose union contains its entire requested interval. Return -1 if that request cannot be covered.
A selected session may begin before the request or end after it. Sessions may overlap, and two sessions that meet at an endpoint provide continuous coverage. Each request is evaluated independently: choosing a session for one request does not consume it for another.
Return the answers in the same order as requests. Neither input is necessarily sorted, and duplicate windows are allowed.
Examples
Example 1
Input: windows = [[0,4],[3,7],[7,10],[1,6],[12,15]], requests = [[0,10],[2,7],[8,13]] Output: [3,2,-1]
The first request can use the sessions [0, 4], [3, 7], and [7, 10]. For the second request, [1, 6] can hand off to [3, 7]. The third request crosses the uncovered gap between times 10 and 12.
Example 2
Input: windows = [[8,12],[-5,8],[-2,3],[-5,8]], requests = [[-4,7],[-5,12],[11,13]] Output: [1,2,-1]
The session [-5, 8] alone contains the first requested span. The second request can continue from that session through [8, 12], since sharing an endpoint is sufficient. No session covers time 13.
Constraints
- 0 <= windows.length <= 100000
- 1 <= requests.length <= 100000
- Every window and request contains exactly two integers.
- -1000000000 <= start < end <= 1000000000 for every window.
- -1000000000 <= left < right <= 1000000000 for every request.
An intended solution takes O((n + q) log(n + 1)) time and O(n log(n + 1)) auxiliary space, where n is the number of windows and q is the number of requests. Time is continuous, not restricted to integer instants.
Hints
Show hint 1Hint 1
If coverage currently reaches time x, which available window gives the best possible reach after selecting one more session?
Show hint 2Hint 2
Every reach after the first selection is a window endpoint. Precompute transitions between those endpoints, then compose powers of two transitions to answer each request.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you reconstruct one minimum-size selection of original window indices for a request?
- How would the solution change if every handoff required a positive overlap of at least d time units?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem