Indices of Ranges Fully Inside a Query Range
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Scan a list of closed integer ranges and return the indices of those lying entirely within a given query range, testing inclusive boundary handling.
Ranges Inside a Query Range
You have a list ranges of closed integer ranges. Each entry is a pair [lo, hi] with lo <= hi. You are also given a query range query = [query_lo, query_hi].
A range [lo, hi] is inside the query range when it does not extend past either end of the query, that is, when both of these hold:
query_lo <= lohi <= query_hi
Both comparisons are inclusive. A range that shares an endpoint with the query still counts, and a range identical to the query counts too.
Return the 0-based positions (indices into ranges) of every range that is inside the query range, in ascending order. If no range qualifies, return an empty list.
Constraints
1 <= len(ranges) <= 10^5ranges[i][0] <= ranges[i][1]for everyi- All endpoint values are integers (they may be negative or large).
queryhas exactly two integers,query[0]andquery[1], withquery[0] <= query[1].- Each index appears at most once in the result.
Examples
Example 1
Input: indices_inside_range([[1, 3], [2, 5], [6, 7], [4, 4]], [1, 5])
Output: [0, 1, 3]
Example 2
Input: indices_inside_range([[10, 20], [1, 5]], [2, 8])
Output: []
Hints
Hint 1
Write down exactly what has to be true about the left end and about the right end of a range for it to lie inside the query.
Hint 2
Because each range satisfies lo <= hi, two inclusive comparisons are enough. Use <= rather than < so touching endpoints count.
Hint 3
Walk through the list once in index order, keeping the index of each range that passes both checks. The output is then automatically ascending.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A range [lo, hi] lies inside [query_lo, query_hi] exactly when query_lo <= lo (it does not start before the query) and hi <= query_hi (it does not end after the query). Each range can be judged independently, so a single pass over the list is enough. Using enumerate keeps track of each original index, and appending indices in iteration order yields an ascending result with no sorting.
Why it is correct
- Because
lo <= hifor every range, these two checks imply the whole range sits between the query endpoints:query_lo <= lo <= hi <= query_hi. No extra check on the other ends is required. - Both comparisons are inclusive, so ranges that touch the query boundary, point ranges (
lo == hi), and a range equal to the query are all accepted correctly. - Every range is tested exactly once, so none is missed and none is reported twice.
- If nothing passes, the result is an empty list.
Complexity
- Time: O(n), with two comparisons per range.
- Space: O(1) auxiliary, plus O(k) for the output, where k is the number of matching ranges.
Reference solution
from typing import List
def indices_inside_range(ranges: List[List[int]], query: List[int]) -> List[int]:
query_lo, query_hi = query
return [
i
for i, (lo, hi) in enumerate(ranges)
if query_lo <= lo and hi <= query_hi
]