HardIntervals · Heap · Sorting

Minimum Interval to Include Each Query

For each query point q, return the length (right − left + 1) of the shortest interval with left ≤ q ≤ right, or −1 if none contains it. Answers go in the original query order.

intervals = [[1,3],[2,3],[3,7],[6,6]]queries = [2,3,1,7,6,8] → [2,2,3,5,1,−1]

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

Number line

Intervals (with length) on top, query points below

Time and space complexity

n = number of intervals, m = number of queries. The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. Up to 8 intervals and 8 queries, values from 1 to 40, written as JSON.