Each closed interval [l, r] has size r − l + 1. For each query point, return the size of the smallest interval containing it, or -1.
Before you code
Answering queries in sorted order, instead of the given order, lets a sweep add each interval once and drop it once. Remember each query's original index to place the answer.
Hints
- Sort intervals by start and queries by value.
- For each query q: push every interval with start ≤ q into a min-heap keyed by size (with its end).
- Pop from the heap while the top's end is less than q: it no longer covers anything ahead.
- The top's size is the answer for q, or -1 if the heap is empty.
Production & complexity notes
When solving Minimum interval to include each query on LeetCode (hard), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Intervals and sweeping to reinforce the underlying pattern.
What it practices
A sweep that maintains a structure of active intervals, from Intervals and sweeping.