Minimum Interval to Include Each Query: the idea to remember.
For each query point, return the length of its shortest containing closed interval, or minus one.
The core insight
Process queries in sorted order, adding intervals that have started and removing expired intervals from a length ordered heap.
Why it works
Every valid interval has been added by the time its query is processed; removing expired heap tops leaves the shortest valid candidate.
The mistake to avoid
Closed intervals have length right minus left plus one. Restore answers to original query order.
Commented Java solution
Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.
int[] minInterval(int[][] intervals, int[] queries) {
Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
Integer[] order = new Integer[queries.length];
for (int i = 0; i < order.length; i++) order[i] = i;
Arrays.sort(order, Comparator.comparingInt(i -> queries[i]));
// Each entry is {interval length, inclusive right endpoint}.
PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
int[] result = new int[queries.length];
int next = 0;
for (int index : order) {
int point = queries[index];
while (next < intervals.length && intervals[next][0] <= point) {
int[] interval = intervals[next++];
heap.add(new int[]{interval[1] - interval[0] + 1, interval[1]});
}
while (!heap.isEmpty() && heap.peek()[1] < point) heap.remove();
result[index] = heap.isEmpty() ? -1 : heap.peek()[0];
}
return result;
}
Make the next connection
Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.