Imagine you're managing a library with time-sensitive book reservations. You have multiple reservation intervals, and patrons are making queries about when they can access books. Your job is to find the shortest reservation period that covers each patron's requested time.
You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] represents the i-th time interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as the number of time units it contains: righti - lefti + 1.
You are also given an integer array queries. For each query queries[j], you need to find the smallest interval that contains this query point. If no such interval exists, return -1.
Goal: Return an array containing the sizes of the smallest intervals for each query, or -1 if no interval contains the query.
Input & Output
Visualization
Time & Space Complexity
For each of the m queries, we check all n intervals
Only using a few variables to track minimum size
Constraints
- 1 โค intervals.length โค 105
- 1 โค queries.length โค 105
- intervals[i].length == 2
- 1 โค lefti โค righti โค 107
- 1 โค queries[j] โค 107
- Large input sizes require efficient O(n log n) solution