BackendML

Minimum Interval to Include Each Query

Hard3 min read
The Minimum Interval to Include Each Query problem requires finding the smallest interval that contains each given query value.

Problem

Given a 2D array of intervals where intervals[i] = [lefti, righti] represents an interval from lefti to righti, and an array of integers queries, return the size of the smallest interval that contains each query.

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

Example(s)

Consider the following examples to understand the expected input and output.

Example 1

Input
intervals = [[1,4],[2,4],[3,6],[4,4]]
queries = [2,3,4,5]
Output
[3,3,1,4]

Example 2

Input
intervals = [[2,3],[2,5],[1,8],[20,25]]
queries = [2,19,5,22]
Output
[2,-1,4,6]

Solution

This solution uses Sorting and a Min Heap. We sort the intervals by their starting point and process the queries in sorted order.

For each query, we add all intervals whose starting point is less than or equal to the query into the min heap. The heap is ordered by interval size, so the smallest interval is always at the top.

Before processing a query, we remove intervals whose ending point is smaller than the query because they cannot contain the current query. The top of the heap then represents the smallest valid interval containing the query.

Since queries are processed in sorted order, each interval is added to and removed from the heap at most once. We store the answers using the original query indices so that the results can be returned in the original order.
class Solution {
    public int[] minInterval(int[][] intervals, int[] queries) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        int[][] sortedQueries = new int[queries.length][2];

        for (int i = 0; i < queries.length; i++) {
            sortedQueries[i][0] = queries[i];
            sortedQueries[i][1] = i;
        }

        Arrays.sort(sortedQueries,
                (a, b) -> Integer.compare(a[0], b[0]));

        PriorityQueue<int[]> minHeap =
                new PriorityQueue<>(
                        (a, b) -> Integer.compare(a[0], b[0])
                );

        int[] result = new int[queries.length];
        Arrays.fill(result, -1);

        int index = 0;
        for (int[] query : sortedQueries) {
            int value = query[0];

            while (index < intervals.length &&
                   intervals[index][0] <= value) {

                int left = intervals[index][0];
                int right = intervals[index][1];
                int size = right - left + 1;

                minHeap.offer(new int[]{size, right});
                index++;
            }

            while (!minHeap.isEmpty() &&
                   minHeap.peek()[1] < value) {
                minHeap.poll();
            }

            if (!minHeap.isEmpty()) {
                result[query[1]] = minHeap.peek()[0];
            }
        }
        return result;
    }
}

Complexity

Sorting the intervals takes O(n log n) time, and sorting the queries takes O(m log m) time, where n is the number of intervals and m is the number of queries.

Each interval is added to and removed from the heap at most once, giving O(n log n) heap operations. Therefore, the overall time complexity is O(n log n + m log m).

The sorted queries, result array, and heap require O(n + m) extra space.

Comments

No comments yet. Ask a question or share what helped.