1020. Smallest Covering Interval
A museum schedules guided tours; tour i runs from minute intervals[i][0] to minute intervals[i][1], both ends included, so its length is right - left + 1. Visitors arrive at various minutes, listed in queries.
A visitor arriving at minute x can join any tour with left <= x <= right, and prefers the shortest such tour. For each query return the length of the shortest tour that is running at that minute, or -1 if no tour is running. The answer array has the same order as queries. Tours may overlap, nest or coincide, and queries may repeat.
Example 1
- Input:
- intervals = [[2,9],[4,5],[7,12],[20,21]]queries = [4,8,11,15,21]
- Output:
- [2,6,6,-1,2]
- Explanation:
Point 4 lies in [2,9] (length 8) and [4,5] (length 2), so 2; point 8 is in [2,9] (length 8) and [7,12] (length 6), so 6; point 11 is only in [7,12] (length 6); 15 is uncovered; 21 is in [20,21] (length 2).
Example 2
- Input:
- intervals = [[1,1],[1,3],[3,3]], queries = [1,2,3,4]
- Output:
- [1,3,1,-1]
- Explanation:
Point 1 is covered by the length-1 interval [1,1]; point 2 only by [1,3] (length 3); point 3 by [3,3] (length 1); point 4 by nothing.
Example 3
- Input:
- intervals = [[5,50]], queries = [50,5,4,51]
- Output:
- [46,46,-1,-1]
- Explanation:
The single interval of length 46 covers 5 and 50 (both ends are included) but not 4 or 51.
Constraints
- 1 ≤ intervals.length ≤ 105
- 1 ≤ queries.length ≤ 105
- 1 ≤ left ≤ right ≤ 107
- 1 ≤ queries[j] ≤ 107
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms
Expected complexity
- Time
- O((n + q) log (n + q))
- Space
- O(n + q)