Skip to content

Minimum Interval to Include Each Query

HardIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

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.

intervals = [[1, 4], [2, 4], [3, 6], [4, 4]], queries = [2, 3, 4, 5]
  →  [3, 3, 1, 4]
 
intervals = [[2, 3], [2, 5], [1, 8], [20, 25]], queries = [2, 19, 5, 22]
  →  [2, -1, 4, 6]

Constraints

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