33 · Intervals

#1851 Minimum Interval to Include Each Query

Hardsolve on LeetCode ↗

frequently reported atGoogle

the problem

In plain words

intervals[i] = [left, right] covers every integer from left to right, and its size is right − left + 1. For each queries[j], find the size of the smallest interval that contains queries[j], or −1 if none does. Return the answers in the queries' original order.

in intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]

out [3,3,1,4]

2 → [2,4] (size 3). 3 → [2,4] (3). 4 → [4,4] (1). 5 → [3,6] (4).

in intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22]

out [2,-1,4,6]

Nothing contains 19.

  • 1 ≤ intervals.length ≤ 10⁵
  • 1 ≤ queries.length ≤ 10⁵
  • 1 ≤ left ≤ right ≤ 10⁷
  • 1 ≤ queries[j] ≤ 10⁷

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #253
      Meeting Rooms II
      Mediumin IntervalsLeetCode Premium
    2. #759
      Employee Free Time
      Hardin IntervalsLeetCode Premium
    3. #973