|
| 1 | +# Minimum Interval to Include Each Query |
| 2 | + |
| 3 | +You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] describes the ith interval starting at |
| 4 | +lefti and ending at righti (inclusive). The size of an interval is defined as the number of integers it contains, or |
| 5 | +more formally righti - lefti + 1. |
| 6 | + |
| 7 | +You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such |
| 8 | +that lefti <= queries[j] <= righti. If no such interval exists, the answer is -1. |
| 9 | + |
| 10 | +- If at least one interval contains the query, return the minimum interval size among those intervals. |
| 11 | +- If no interval contains the query, return -1 for that query. |
| 12 | + |
| 13 | +Return an array containing the answers to the queries. |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Example 4 |
| 22 | + |
| 23 | +```text |
| 24 | +Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5] |
| 25 | +Output: [3,3,1,4] |
| 26 | +Explanation: The queries are processed as follows: |
| 27 | +- Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3. |
| 28 | +- Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3. |
| 29 | +- Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1. |
| 30 | +- Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4. |
| 31 | +``` |
| 32 | + |
| 33 | +Example 5 |
| 34 | +```text |
| 35 | +Input: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22] |
| 36 | +Output: [2,-1,4,6] |
| 37 | +Explanation: The queries are processed as follows: |
| 38 | +- Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2. |
| 39 | +- Query = 19: None of the intervals contain 19. The answer is -1. |
| 40 | +- Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4. |
| 41 | +- Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6. |
| 42 | +``` |
| 43 | + |
| 44 | +## Constraints |
| 45 | + |
| 46 | +- 1 <= intervals.length <= 10^5 |
| 47 | +- 1 <= queries.length <= 10^5 |
| 48 | +- intervals[i].length == 2 |
| 49 | +- 1 <= lefti <= righti <= 10^7 |
| 50 | +- 1 <= queries[j] <= 10^7 |
| 51 | + |
| 52 | +## Topics |
| 53 | + |
| 54 | +- Array |
| 55 | +- Binary Search |
| 56 | +- Sweep Line |
| 57 | +- Sorting |
| 58 | +- Heap (Priority Queue) |
| 59 | + |
| 60 | +## Hints |
| 61 | + |
| 62 | +- Is there a way to order the intervals and queries such that it takes less time to query? |
| 63 | +- Is there a way to add and remove intervals by going from the smallest query to the largest query to find the minimum |
| 64 | + size? |
| 65 | + |
| 66 | +## Solution |
| 67 | + |
| 68 | +The core idea is to process the queries from smallest to largest, while continuously keeping track of which intervals |
| 69 | +are active candidates for the current query. As we move forward through increasing query values, more intervals become |
| 70 | +eligible because their left endpoint is now less than equal to the current query, so we add them to a min heap. At the |
| 71 | +same time, some previously added intervals may stop being useful because their right endpoints are now less than the |
| 72 | +query, meaning they can no longer cover the query (or any future larger query), so we remove them. The heap is ordered |
| 73 | +by interval size (smallest first), and we store each interval’s right endpoint alongside its size; this lets us quickly |
| 74 | +discard expired intervals and ensures that after cleanup, the heap’s top element always represents the smallest-size |
| 75 | +interval that still covers the current query. This gives an efficient way to answer each query without scanning all |
| 76 | +intervals repeatedly. |
| 77 | + |
| 78 | +The steps of the algorithm are as follows: |
| 79 | + |
| 80 | +1. Sort intervals by starting point (`left`) so we can add them in the correct order as queries increase. |
| 81 | +2. Attach original indexes to queries as (`queryValue`, `originalIndex`), then sort queries by `queryValue` so we |
| 82 | + process them from smallest to largest. |
| 83 | +3. Initialize the `ans` array filled with -1 |
| 84 | +4. Initialize a min heap, `heap`, that stores interval size and right endpoint as tuples `(size, right)`. |
| 85 | +5. Initialize a pointer i = 0 to walk through the sorted intervals |
| 86 | +6. For each query q in sorted order: |
| 87 | + - **Add all intervals starting at or before q**: While intervals[i].left <= q, push (size, right) into the heap and |
| 88 | + increment i. |
| 89 | + - **Remove invalid intervals**: While heap top has `right < q`, pop it (it cannot cover `q` anymore). |
| 90 | + - Answer the query: |
| 91 | + - If the heap is non-empty, the top element’s `size` is the smallest interval covering `q`, so store it in the `ans` |
| 92 | + array at the query’s original index. |
| 93 | + - Otherwise, leave `ans` as -1. |
| 94 | +7. Return ans in the original query order. |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +## Complexity Analysis |
| 112 | + |
| 113 | +### Time Complexity |
| 114 | + |
| 115 | +The algorithm's time complexity is dominated by sorting and heap operations. Sorting the `intervals` array takes |
| 116 | +`O(nlog(n))`, where `n` is the number of intervals, and sorting the queries (after pairing each query with its original |
| 117 | +index) takes `O(mlog(m))`, where `m` is the number of queries. |
| 118 | + |
| 119 | +During the sweep through the sorted queries, each interval is pushed into the min heap at most once, and popped at most |
| 120 | +once, and each heap operation costs `O(log(n))`, giving a total heap cost of `O(nlog(n))`. |
| 121 | + |
| 122 | +Altogether, the overall time complexity is `O(n log(n) + m log(m) + n log(n))`, which is commonly simplified to |
| 123 | +`O((n+m) log(n))` (or equivalently O(n log (n) + m log(m))). |
| 124 | + |
| 125 | +### Space Complexity |
| 126 | + |
| 127 | +The space complexity is `O(n+m)` because, in the worst case, the heap can hold up to `n` intervals, and we also store |
| 128 | +the sorted query list and the answer array, each of size `m`. |
0 commit comments