Skip to content
Problem 1851 · LeetCode 1851(opens in a new tab)

Minimum Interval to Include Each Query

HardHeaps

You get a list of intervals, each [left, right] covering every whole number from left to right, ends included. Its size is how many whole numbers it covers: right − left + 1.

You also get a list of queries. For each query, find the smallest size among the intervals that cover it, or −1 if no interval does. Return the answers in the same order as the queries.

Example 1

Input:  intervals = [[2,5],[1,8],[4,4],[6,7]], queries = [4,7,9,1]
Output: [1, 2, -1, 8]
Why:    4 is in [4,4]; 7 in [6,7]; nothing holds 9; 1 only in [1,8]
2 more examples and the constraints

Example 2

Input:  intervals = [[5,9],[10,10],[3,12]], queries = [10,2,12,6]
Output: [1, -1, 10, 5]
Why:    10 is in [10,10]; 2 is in none; 6 is in [5,9]

Example 3

Input:  intervals = [[1,1]], queries = [1,1,2]
Output: [1, 1, -1]
Why:    a query can repeat; each one gets an answer

Constraints

  • 1 ≤ number of intervals ≤ 100,000
  • 1 ≤ number of queries ≤ 100,000
  • 1 ≤ left ≤ right ≤ 10,000,000, and 1 ≤ each query ≤ 10,000,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Brute forceO(n · q)
  2. Sort + min-heap sweepO(n log n + q log q)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 of eachVerdict
Brute forceO(n · q)
Mention it, then move on
Sort + min-heap sweepO(n log n + q log q)
Best; each interval in and out once

n = number of intervals, q = number of queries.

Edge cases to test

  • No interval covers a query. Query 9 in Example 1 must give −1, even after the heap held intervals.
  • Repeated queries. [1, 1, 2] over [[1, 1]]: both 1s get 1. Sort query positions, not values, so neither is lost.
  • Interval ends exactly at the query. [4, 4] covers 4. Pop only when right < q, not right ≤ q.
  • Huge interval. [1, 10,000,000] has size 10,000,000; it still fits in a 32-bit integer.

Common mistakes

  • Answering in sorted order but writing answers to the sorted position instead of the original one.
  • Popping intervals that are not on top, or never popping, so a stale small interval answers a later query.
  • Keying the heap on the right end instead of the size.

How to talk through it

  1. 1Clarify. Are both ends inclusive? Can queries repeat? Do I return answers in the original order?
  2. 2Give the brute force. Scan every interval per query: O(n · q), far too slow at 100,000 each.
  3. 3Go offline. All queries are known, so sort them. Then intervals only ever start counting, and once ended they stay ended.
  4. 4Code the sweep. Push by left end, pop by right end, read the top. Keep the original index of each query.
  5. 5Test out loud. Run a query that nothing covers, a repeated query and an interval that ends on the query.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?