Minimum Interval to Include Each Query
Beginner Mode

Problem Statement

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

You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such that lefti <= queries[j] <= righti. If no such interval exists, the answer is -1.

Return an array containing the answers to the queries.

Additional information

  • 1 <= intervals.length <= 10^5
  • 1 <= queries.length <= 10^5
  • intervals[i].length == 2
  • 1 <= lefti <= righti <= 10^7
  • 1 <= queries[j] <= 10^7

Example 1:

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 intervals containing 2 are [1,4] and [2,4]. The smallest is [2,4] with size 3.
  • Query 3: The intervals containing 3 are [1,4], [2,4], and [3,6]. The smallest is [2,4] with size 3.
  • Query 4: The intervals containing 4 are [1,4], [2,4], [3,6], and [4,4]. The smallest is [4,4] with size 1.
  • Query 5: The interval containing 5 is [3,6]. The size is 4.

Example 2:

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 intervals containing 2 are [2,3], [2,5], and [1,8]. The smallest is [2,3] with size 2.
  • Query 19: None of the intervals contain 19.
  • Query 5: The intervals containing 5 are [2,5] and [1,8]. The smallest is [2,5] with size 4.
  • Query 22: The interval containing 22 is [20,25]. The size is 6.
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →