Problem
Ask
Submissions

Problem: Minimum Interval to Include Each Query

Hard
40 min
Explore how to identify the minimum-sized interval that contains each query value from a set of intervals. Learn to handle interval coverage problems using logical and coding techniques to efficiently answer queries within constraints.

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=rightilefti+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 leftiqueries[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:

  • 11 \leq intervals.length 105\leq 10^5

  • 11 \leq queries.length 105\leq 10^5

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

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

  • 11 \leq queries[j] 107\leq 10^7

Problem
Ask
Submissions

Problem: Minimum Interval to Include Each Query

Hard
40 min
Explore how to identify the minimum-sized interval that contains each query value from a set of intervals. Learn to handle interval coverage problems using logical and coding techniques to efficiently answer queries within constraints.

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=rightilefti+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 leftiqueries[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:

  • 11 \leq intervals.length 105\leq 10^5

  • 11 \leq queries.length 105\leq 10^5

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

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

  • 11 \leq queries[j] 107\leq 10^7