包含每个查询的最小区间
难度:
标签:
题目描述
You are given a 2D integer array intervals
, where intervals[i] = [lefti, righti]
describes the ith
interval starting at lefti
and ending at righti
(inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1
.
You are also given an integer array queries
. The answer to the jth
query is the size of the smallest interval i
such that lefti <= queries[j] <= righti
. If no such interval exists, the answer is -1
.
Return an array containing the answers to the queries.
Example 1:
Input: intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5] Output: [3,3,1,4] Explanation: The queries are processed as follows: - Query = 2: The interval [2,4] is the smallest interval containing 2. The answer is 4 - 2 + 1 = 3. - Query = 3: The interval [2,4] is the smallest interval containing 3. The answer is 4 - 2 + 1 = 3. - Query = 4: The interval [4,4] is the smallest interval containing 4. The answer is 4 - 4 + 1 = 1. - Query = 5: The interval [3,6] is the smallest interval containing 5. The answer is 6 - 3 + 1 = 4.
Example 2:
Input: intervals = [[2,3],[2,5],[1,8],[20,25]], queries = [2,19,5,22] Output: [2,-1,4,6] Explanation: The queries are processed as follows: - Query = 2: The interval [2,3] is the smallest interval containing 2. The answer is 3 - 2 + 1 = 2. - Query = 19: None of the intervals contain 19. The answer is -1. - Query = 5: The interval [2,5] is the smallest interval containing 5. The answer is 5 - 2 + 1 = 4. - Query = 22: The interval [20,25] is the smallest interval containing 22. The answer is 25 - 20 + 1 = 6.
Constraints:
1 <= intervals.length <= 105
1 <= queries.length <= 105
intervals[i].length == 2
1 <= lefti <= righti <= 107
1 <= queries[j] <= 107
代码结果
运行时间: 291 ms, 内存: 50.9 MB
/*
* 思路:
* 使用Java Stream API来实现同样的逻辑。我们可以使用Stream进行排序、过滤以及收集操作。
* 在这里,我们可以使用Map来保存每个查询点的答案。
*/
import java.util.*;
import java.util.stream.*;
public class SolutionStream {
public int[] minInterval(int[][] intervals, int[] queries) {
Map<Integer, Integer> queryResults = new TreeMap<>();
// 将queries中的查询点和原索引对应保存
IntStream.range(0, queries.length).forEach(i -> queryResults.put(queries[i], -1));
// 对区间按起点升序,起点相同时按终点升序排序
Arrays.sort(intervals, Comparator.comparingInt((int[] a) -> a[0]).thenComparingInt(a -> a[1]));
// 处理每个查询点
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
int index = 0;
for (int query : queryResults.keySet()) {
while (index < intervals.length && intervals[index][0] <= query) {
int left = intervals[index][0];
int right = intervals[index][1];
minHeap.offer(new int[]{right - left + 1, right});
index++;
}
while (!minHeap.isEmpty() && minHeap.peek()[1] < query) {
minHeap.poll();
}
queryResults.put(query, minHeap.isEmpty() ? -1 : minHeap.peek()[0]);
}
// 返回结果
return Arrays.stream(queries).map(queryResults::get).toArray();
}
}
解释
方法:
此题解采用排序和优先队列(堆)的方法来解决问题。首先,对查询按值进行排序,并保留原始索引以便最后能正确地放置答案。接着,对区间按起始位置进行排序。对于每个查询,使用最小堆来维护所有包含当前查询点的区间,并确保堆中区间的结束位置至少大于等于查询值。堆中存储元组,包含区间长度、起始位置和结束位置,按长度排序以快速获得最小长度的区间。对于每个查询,将所有起始位置小于等于查询值的区间加入堆中,然后移除堆中所有结束位置小于查询值的区间。如果堆非空,堆顶元素即为包含该查询值的最小区间长度。
时间复杂度:
O((n+m) log n)
空间复杂度:
O(n + m)
代码细节讲解
🦆
请问为什么在处理查询之前要对区间和查询值分别进行排序?这样的排序有什么具体的好处?
▷🦆
在题解中,使用最小堆存储区间时,为什么选择按区间长度进行排序而不是按结束位置或开始位置排序?
▷🦆
在移除堆中结束位置小于当前查询值的区间时,可能会有哪些对算法性能的影响?是否存在更效率的方法来维护这个堆的状态?
▷