> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Interval to Include Each Query

> Tested Python solution for LeetCode 1851 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1851, Hard. Topics: Array, Binary Search, Sweep Line, Sorting, Heap (Priority Queue). [View on LeetCode](https://leetcode.com/problems/minimum-interval-to-include-each-query/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1851   # by problem number
lcpy gen -s minimum_interval_to_include_each_query   # by problem name
```

## Problem

You are given a 2D integer array `intervals`, where `intervals[i] = [left_i, right_i]` describes the `i^th` interval starting at `left_i` and ending at `right_i` **(inclusive)**. The **size** of an interval is defined as the number of integers it contains, or more formally `right_i - left_i + 1`.

You are also given an integer array `queries`. The answer to the `j^th` query is the **size of the smallest interval** `i` such that `left_i <= queries[j] <= right_i`. If no such interval exists, the answer is `-1`.

Return *an array containing the answers to the queries*.

### Examples

```
Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]
Output: [3,3,1,4]
Explanation: The queries are processed as follows:
- Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3.
- Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3.
- Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1.
- Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.
```

```
Input: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]
Output: [2,-1,4,6]
Explanation: The queries are processed as follows:
- Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2.
- Query = 19: None of the intervals contain 19. The answer is -1.
- Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4.
- Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_interval_to_include_each_query/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_interval_to_include_each_query/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq


class Solution:
    # Time: O((n + q) log n) - n intervals, q queries
    # Space: O(n)
    def min_interval(self, intervals: list[list[int]], queries: list[int]) -> list[int]:
        intervals.sort()
        sorted_queries = sorted(range(len(queries)), key=lambda i: queries[i])

        result = [-1] * len(queries)
        min_heap: list[tuple[int, int]] = []  # (size, right)
        interval_idx = 0

        for query_idx in sorted_queries:
            query_val = queries[query_idx]

            # Add all intervals that start at or before this query value.
            while interval_idx < len(intervals) and intervals[interval_idx][0] <= query_val:
                left, right = intervals[interval_idx]
                heapq.heappush(min_heap, (right - left + 1, right))
                interval_idx += 1

            # Remove intervals that ended before this query value.
            while min_heap and min_heap[0][1] < query_val:
                heapq.heappop(min_heap)

            if min_heap:
                result[query_idx] = min_heap[0][0]

        return result
```

## Complexity

| Time                                      | Space |
| ----------------------------------------- | ----- |
| O((n + q) log n) - n intervals, q queries | O(n)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
