Minimum Interval to Include Each Query

Finding the Minimum Interval to Include Each Query

Handling ranges and efficiently answering queries based on given intervals is a common challenge in programming.

This article explores how to determine the shortest interval that includes a specific query point. If no such interval exists, the result should indicate that explicitly.

Problem Description

You are given:

  1. A 2D array intervals, where each interval is represented as [left, right].
    • These intervals are inclusive, meaning a point pp is considered within an interval if left≤p≤rightleft \leq p \leq right.
  2. An array of queries, where each query asks for the shortest interval that includes the query point.

Your Task:

For each query qq, return:

  • The length of the shortest interval that includes qq.
  • If no interval contains qq, return -1.

Key Notes:

  • The length of an interval [left,right][left, right] is calculated as length=right−left+1\text{length} = \text{right} – \text{left} + 1.

Explanation:

  • Query = 2: The interval [2,3]  is the smallest one containing 2, it’s length is 2.
  • Query = 3: The interval [2,3]  is the smallest one containing 3, it’s length is 2.
  • Query = 1: The interval [1,3]  is the smallest one containing 1, it’s length is 3.
  • Query = 7: The interval [3,7]  is the smallest one containing 7, it’s length is 5.
  • Query = 6: The interval [6,6]  is the smallest one containing 6, it’s length is 1.
  • Query = 8: There is no interval containing 8.

Constraints:

  • 1 <= intervals.length <= 1000
  • 1 <= queries.length <= 1000
  • 1 <= left_i <= right_i <= 10000
  • 1 <= queries[j] <= 10000

Key Idea:

  1. Sort Intervals and Queries:
    Sorting intervals by length ensures that we evaluate shorter intervals first. Sorting queries allows us to process them efficiently in ascending order.

  2. Use a Min-Heap to Track Active Intervals:
    As we process each query, maintain a min-heap of intervals that could potentially include the query point. Remove intervals from the heap when they can no longer contain the current query.

  3. Binary Search or Sliding Window for Interval Selection:
    Efficiently match intervals to queries by leveraging their sorted order.

Algorithm

  1. Preprocessing:

    • Sort intervals by length (right−left+1\text{right} – \text{left} + 1).
    • Sort queries while keeping track of their original indices.
  2. Initialize a Min-Heap:

    • Use the heap to store intervals currently valid for the query. The heap stores intervals as (length,right,left)(\text{length}, \text{right}, \text{left}).
  3. Process Queries:

    • For each query in sorted order:
      • Add intervals to the heap if the interval’s left is less than or equal to the query.
      • Remove intervals from the heap if the interval’s right is less than the query.
      • If the heap is not empty, the top of the heap gives the shortest valid interval for the query. Otherwise, return -1.
  4. Output the Results:

    • Return the results in the original query order.

There are mainly Four  approach to solve this problem – 

  1. Brute Force
  2. Sweep Line Algorithm
  3. Min Heap
  4. Min Segment Tree (Lazy Propagation)

1. Brute Force  

  • Time complexity: O(m∗n)
  • Space complexity:  O(1)

2. Sweep Line Algorithm

Time & Space Complexity
  • Time complexity:  O((n+m)log⁡(n+m))
  • Space complexity:  O(n+m)

Where m is the length of the array queries and n is the length of the array intervals.

3. Min Heap

  • Time complexity: O(nlogn+mlogm)
  • Space complexity: O(n+m)

4. Min Segment Tree (Lazy Propagation)

  • Time complexity: O(nlogn)
  • Space complexity: O(n)

More Articles