Minimum Interval to Include Each Query
Find the smallest interval that contains each query point
DSA, Hard, Intervals
Which structure or algorithm, and what does it cost.
A highway has service windows in a list windows, each [left, right]. A window covers every position from left to right, both included. Its size is right - left + 1, the number of positions it covers. A list queries holds customer positions. For each position, find the smallest window that covers it and report that size. If no window covers it, report -1. Return the answers in the same order as queries.
Examples
Constraints
- 1 <= length of
windows<= 50,000, and 1 <= length ofqueries<= 50,000. - 0 <= left <= right <= 1,000,000, and 0 <= each query <= 1,000,000.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O((n + q) log(n + q)) time, with n windows and q queries.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Checking every window for every query is too slow. Does the order in which you answer the queries have to be the order they were given?
Hint 2 the approach
Answer the queries from the smallest position to the largest, and remember each query's original place. Move through the windows sorted by left end as the position grows.
Hint 3 the key step
Keep the windows that have started in a min-heap ordered by size. What must you check about the top before you trust it?
Walkthrough
Full walkthrough brute force to best
Brute force
For each query, scan every window and keep the smallest one that covers it. That is O(n * q) time: about 2.5 billion checks for 50,000 windows and 50,000 queries.
Best method
Sort the windows by left end. Sort the query positions from small to large, remembering each one's original index. Keep a min-heap of windows that have started, ordered by size, with the right end stored too. For a query q, first push every window whose left end is at most q. Then pop the top while its right end is below q. A window that ended before q also ended before every later query, so it can never help again, and it is safe to drop it for good. Now the top covers q. Any window that covers q has left at most q and right at least q, so it is in the heap, and smaller ones above it were dropped only because they could not cover q. So the top is the smallest. Its size is the answer, or -1 if the heap is empty. Write it to the query's original place.
Hand trace
Take windows [[2,8],[4,5],[7,12],[10,11]] and queries [9,6,1,11,5]. Sorted queries: 1, 5, 6, 9, 11. Query 1: no window starts at or before 1, so the heap is empty and the answer is -1. Query 5: push [2,8] as size 7 and [4,5] as size 2. The top has size 2 and right end 5, which is at least 5, so the answer is 2. Query 6: the top has right end 5, below 6, so drop it. The top is [2,8], size 7. Query 9: push [7,12] as size 6. The top is size 6. Query 11: push [10,11] as size 2. The top is size 2. By original place, the answers are [6, 7, -1, 2, 2].
Code
import heapq
def shortest_windows(windows, queries):
windows = sorted(windows)
answer = [-1] * len(queries)
heap = []
i = 0
for k in sorted(range(len(queries)), key=lambda k: queries[k]):
q = queries[k]
while i < len(windows) and windows[i][0] <= q:
left, right = windows[i]
heapq.heappush(heap, (right - left + 1, right))
i += 1
while heap and heap[0][1] < q:
heapq.heappop(heap)
if heap:
answer[k] = heap[0][0]
return answerTime and memory
O((n + q) log(n + q)) time: both sorts, and each window is pushed and popped at most once at O(log n) each. O(n + q) memory for the heap, the query order and the answers.
Common mistakes
- Dropping a window whose right end equals the query. Both ends are included, so the window still covers it. Drop only when the right end is below the query.
- Reading only the top of the heap without dropping windows that already ended. A short window to the left of the query would be reported as covering it.
- Writing the answers in sorted-query order. Keep each query's original index and write the answer there.