Tap here to switch tabs
Problem
Submissions

Problem: Minimum Interval to Include Each Query

Minimum Interval to Include Each Query asks you to find the smallest interval containing each query value, returning its size or negative one when no interval applies.

Statement

You are given a 2D integer array, intervals, where each element intervals[i] = [lefti, righti][left_i, \space right_i] represents the ithi^{th} interval on the number line. Each interval includes all integers from leftileft_i to rightiright_i, inclusive. The size (length) of an interval [lefti, righti][left_i, \space right_i] is defined as size=righti−lefti+1\text{size} = right_i - left_i + 1.

You are also given an integer array, queries. For each query value queries[j], you must find the smallest-sized interval [lefti, righti][left_i, \space right_i] such that lefti≤queries[j]≤rightileft_i \le queries[j] \le right_i.

  • If at least one interval contains the query, return the minimum interval size among those intervals.

  • If no interval contains the query, return -1 for that query.

Return an array, answer, where answer[j] is the result for queries[j].

Constraints:

  • 1≤1 \leq intervals.length ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • intervals[i].length ==2== 2

  • 1≤lefti1 \leq left_i ≤righti\leq right_i ≤107\leq 10^7

  • 1≤1 \leq queries[j] ≤107\leq 10^7

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Interval to Include Each Query

Minimum Interval to Include Each Query asks you to find the smallest interval containing each query value, returning its size or negative one when no interval applies.

Statement

You are given a 2D integer array, intervals, where each element intervals[i] = [lefti, righti][left_i, \space right_i] represents the ithi^{th} interval on the number line. Each interval includes all integers from leftileft_i to rightiright_i, inclusive. The size (length) of an interval [lefti, righti][left_i, \space right_i] is defined as size=righti−lefti+1\text{size} = right_i - left_i + 1.

You are also given an integer array, queries. For each query value queries[j], you must find the smallest-sized interval [lefti, righti][left_i, \space right_i] such that lefti≤queries[j]≤rightileft_i \le queries[j] \le right_i.

  • If at least one interval contains the query, return the minimum interval size among those intervals.

  • If no interval contains the query, return -1 for that query.

Return an array, answer, where answer[j] is the result for queries[j].

Constraints:

  • 1≤1 \leq intervals.length ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • intervals[i].length ==2== 2

  • 1≤lefti1 \leq left_i ≤righti\leq right_i ≤107\leq 10^7

  • 1≤1 \leq queries[j] ≤107\leq 10^7