Program to find minimum interval to include each query in Python

Given a list of intervals and queries, we need to find the smallest interval that contains each query value. For each query, we return the size of the smallest interval that contains it, or -1 if no such interval exists.

Problem Understanding

Each interval is represented as [left, right] where both endpoints are inclusive. For a query value q, we need to find the interval with minimum size where left ? q ? right. The size of an interval [left, right] is right - left + 1.

Example

If the input is intervals = [[2,5],[3,5],[4,7],[5,5]] and queries = [3,4,5,6], then the output will be [3, 3, 1, 4] because ?

  • For query = 3: The interval [3,5] is the smallest one containing 3, so size = 5 - 3 + 1 = 3.

  • For query = 4: The interval [3,5] is the smallest one containing 4, so size = 5 - 3 + 1 = 3.

  • For query = 5: The interval [5,5] is the smallest one containing 5, so size = 5 - 5 + 1 = 1.

  • For query = 6: The interval [4,7] is the smallest one containing 6, so size = 7 - 4 + 1 = 4.

Algorithm

We use a heap-based approach to efficiently find the minimum interval for each query ?

  1. Sort intervals in reverse order by start time

  2. Process queries in sorted order

  3. Use a min-heap to track valid intervals by their size

  4. For each query, add qualifying intervals to heap and remove expired ones

Implementation

import heapq

def solve(intervals, queries):
    intervals = sorted(intervals)[::-1]
    h = []
    res = {}
    
    for q in sorted(queries):
        # Add intervals that start before or at q
        while intervals and intervals[-1][0] <= q:
            i, j = intervals.pop()
            if j >= q:
                heapq.heappush(h, [j - i + 1, j])
        
        # Remove intervals that end before q
        while h and h[0][1] < q:
            heapq.heappop(h)
        
        # Store result for this query
        res[q] = h[0][0] if h else -1
    
    return [res[q] for q in queries]

# Test the solution
intervals = [[2,5],[3,5],[4,7],[5,5]]
queries = [3,4,5,6]
print(solve(intervals, queries))
[3, 3, 1, 4]

How It Works

The algorithm processes queries in sorted order to maintain efficiency. For each query q ?

  • Add valid intervals: Pop intervals from the sorted list where start ? q and end ? q, adding them to the min-heap

  • Remove expired intervals: Remove intervals from heap where end

  • Find minimum: The top of the heap contains the smallest valid interval

Time Complexity

The time complexity is O((n + m) log n) where n is the number of intervals and m is the number of queries. Sorting takes O(n log n + m log m), and heap operations take O(n log n) in total.

Conclusion

This solution efficiently finds the minimum interval containing each query using a heap-based approach. By processing queries in sorted order and maintaining a min-heap of valid intervals, we achieve optimal performance for this interval query problem.

---
Updated on: 2026-03-26T14:51:29+05:30

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements