You get an array intervals and an array queries.
Each interval is a pair [left, right]. It holds every whole number from left to right, both ends included. Its size is right - left + 1, the count of numbers it holds.
For each query, find the smallest interval that holds it. Return an array with the sizes of these intervals, in the order of queries. If no interval holds a query, its answer is -1.
Example 1:
Input: intervals = [[2,6],[4,5],[1,9],[8,8]], queries = [5,8,1,3] Output: [2,1,9,5] Explanation: 5 is in [2,6], [4,5] and [1,9]. The smallest is [4,5], size 2. 8 is in [1,9] and [8,8]. The smallest is [8,8], size 1. 1 is only in [1,9], size 9. 3 is in [2,6] and [1,9]. The smallest is [2,6], size 5.
Example 2:
Input: intervals = [[10,12],[30,31]], queries = [11,20,31,9] Output: [3,-1,2,-1] Explanation: 11 is in [10,12], size 3. No interval holds 20 or 9, so their answers are -1. 31 is in [30,31], size 2.
Example 3:
Input: intervals = [[1,3],[3,5],[3,3]], queries = [3,3,4] Output: [1,1,3] Explanation: Both ends count, so 3 is in all three intervals. The smallest is [3,3], size 1. The second 3 gets the same answer. 4 is only in [3,5], size 3.
Constraints:
- 1 to 100,000 intervals.
- 1 to 100,000 queries.
- Each
leftandrightis a whole number from 1 to 10,000,000. leftis never greater thanright.- Each query is a whole number from 1 to 10,000,000.
- Intervals can overlap or repeat.
- Both arrays come in any order.
Note:
Your solution should run in O(n log n + q log q) time, for n intervals and q queries.
Free plan: 30 tutor messages. Premium is unlimited.
