Skip to content
Decoder

Minimum Interval to Include Each Query

Hard

Problem

intervals[i] = [left, right] is inclusive, and its size is right - left + 1. For each value in queries, find the smallest interval that contains it and report that interval's size, or -1 if no interval contains it. Return the answers in the original query order.

Example 1

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

Example 2

  →  [3, 3, 1, 4]

Example 3

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

Example 4

  →  [2, -1, 4, 6]

Constraints

  • 1 <= intervals.length, queries.length <= 10^5
  • 1 <= left <= right <= 10^7
  • 1 <= queries[j] <= 10^7

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.