150NeetCode 150

135.Minimum Interval to Include Each Query

HardIntervals · 6/6

Problem

You are given a 2D array intervals where intervals[i] = [left_i, right_i] describes the inclusive interval from left_i to right_i. Its size is right_i - left_i + 1.

You are also given an array queries. For each queries[j], find the size of the smallest interval i with left_i <= queries[j] <= right_i. If no interval contains the query, the answer is -1. Return the answers in the original query order.

Examples

Input:  intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
Output: [3,3,1,4]
Explanation:
- 2: [2,4] has size 3 (smallest containing 2).
- 3: [2,4] has size 3.
- 4: [4,4] has size 1.
- 5: only [3,6] contains it, size 4.
Input:  intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
Output: [2,-1,4,6]
Explanation: 19 is in no interval; 22 is only in [20,25] (size 6).

Constraints

  • 1 <= intervals.length <= 10^5
  • 1 <= queries.length <= 10^5
  • intervals[i].length == 2
  • 1 <= left_i <= right_i <= 10^7
  • 1 <= queries[j] <= 10^7

💡 Key Insight

Answer the queries offline in increasing order. Sort intervals by left endpoint; as the query value grows, push every interval that has started (left <= q) into a min-heap keyed by size. Intervals at the top that have already ended (right < q) can be popped permanently — later queries are even larger, so they will never contain them again. The heap top is then the smallest interval containing q.

Brute Force (Scan All Intervals per Query)

TimeO(n · q)SpaceO(1) besides the output.

Intuition

For every query, check every interval and keep the smallest size among those containing the query.

Algorithm

  1. For each query q: best = infinity.
  2. For each interval [l, r]: if l <= q <= r, best = min(best, r - l + 1).
  3. Store best, or -1 if nothing matched.

Visualization

q = 5, intervals [1,4] [2,4] [3,6] [4,4]
[1,4]: 5 > 4 no    [2,4]: no    [3,6]: yes size 4    [4,4]: no
answer 4

Code

java
class Solution {
    public int[] minInterval(int[][] intervals, int[] queries) {
        int[] ans = new int[queries.length];
        for (int j = 0; j < queries.length; j++) {
            int q = queries[j];
            int best = Integer.MAX_VALUE;
            for (int[] in : intervals) {
                if (in[0] <= q && q <= in[1]) {
                    best = Math.min(best, in[1] - in[0] + 1);
                }
            }
            ans[j] = best == Integer.MAX_VALUE ? -1 : best;
        }
        return ans;
    }
}

Complexity

  • Time: O(n · q) — up to 10^10 checks at the limits, far too slow.
  • Space: O(1) besides the output.

Complexity Comparison

Approach Time Space
Scan All Intervals per Query O(n · q) O(1)
Sort by Size + Early Exit O(n log n + n · q) O(n)
Sort + Sweep with Min-Heap O(n log n + q log q) O(n + q)

Common Pitfalls

  • Answering queries in sorted order but writing answers at the sorted position — store each query's original index.
  • Popping an interval because it doesn't contain the current query when it simply hasn't started yet — only push intervals with left <= q, and only pop those with right < q.
  • Forgetting the + 1 in size: intervals are inclusive, so [4,4] has size 1.
  • Duplicate query values: the heap logic handles them naturally, but a HashMap<value, answer> shortcut must not lose any original index.