Skip to content

Commit 305107e

Browse files
authored
feat: add solutions to lc problem: No.1851 (#1235)
No.1851.Minimum Interval to Include Each Query
1 parent 1470e0a commit 305107e

File tree

6 files changed

+376
-2
lines changed

6 files changed

+376
-2
lines changed

solution/1800-1899/1851.Minimum Interval to Include Each Query/README.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,160 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:排序 + 离线查询 + 优先队列(小根堆)**
58+
59+
我们注意到,题目中查询的顺序并不会影响答案,并且涉及到的区间也不会发生变化,因此,我们考虑将所有的查询按照从小到大的顺序进行排序,同时将所有的区间按照左端点从小到大的顺序进行排序。
60+
61+
我们使用一个优先队列(小根堆) $pq$ 来维护当前所有的区间,队列的每个元素是一个二元组 $(v, r)$,表示一个区间长度为 $v$,右端点为 $r$ 的区间。初始时,优先队列为空。另外,我们定义一个指针 $i$,指向当前遍历到的区间,初始时 $i=0$。
62+
63+
我们按照从小到大的顺序依次遍历每个查询 $(x, j)$,并进行如下操作:
64+
65+
- 如果指针 $i$ 尚未遍历完所有的区间,并且当前遍历到的区间 $[a, b]$ 的左端点小于等于 $x$,那么我们将该区间加入优先队列中,并将指针 $i$ 后移一位,循环此过程;
66+
- 如果优先队列不为空,并且堆顶元素的右端点小于 $x$,那么我们将堆顶元素弹出,循环此过程;
67+
- 此时,如果优先队列不为空,那么堆顶元素就是包含 $x$ 的最小区间。我们将其长度 $v$ 加入答案数组 $ans$ 中。
68+
69+
在上述过程结束后,我们返回答案数组 $ans$ 即可。
70+
71+
时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $intervals$ 和 $queries$ 的长度。
72+
5773
<!-- tabs:start -->
5874

5975
### **Python3**
6076

6177
<!-- 这里可写当前语言的特殊实现逻辑 -->
6278

6379
```python
64-
80+
class Solution:
81+
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
82+
n, m = len(intervals), len(queries)
83+
intervals.sort()
84+
queries = sorted((x, i) for i, x in enumerate(queries))
85+
ans = [-1] * m
86+
pq = []
87+
i = 0
88+
for x, j in queries:
89+
while i < n and intervals[i][0] <= x:
90+
a, b = intervals[i]
91+
heappush(pq, (b - a + 1, b))
92+
i += 1
93+
while pq and pq[0][1] < x:
94+
heappop(pq)
95+
if pq:
96+
ans[j] = pq[0][0]
97+
return ans
6598
```
6699

67100
### **Java**
68101

69102
<!-- 这里可写当前语言的特殊实现逻辑 -->
70103

71104
```java
105+
class Solution {
106+
public int[] minInterval(int[][] intervals, int[] queries) {
107+
int n = intervals.length, m = queries.length;
108+
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
109+
int[][] qs = new int[m][0];
110+
for (int i = 0; i < m; ++i) {
111+
qs[i] = new int[] {queries[i], i};
112+
}
113+
Arrays.sort(qs, (a, b) -> a[0] - b[0]);
114+
int[] ans = new int[m];
115+
Arrays.fill(ans, -1);
116+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
117+
int i = 0;
118+
for (int[] q : qs) {
119+
while (i < n && intervals[i][0] <= q[0]) {
120+
int a = intervals[i][0], b = intervals[i][1];
121+
pq.offer(new int[] {b - a + 1, b});
122+
++i;
123+
}
124+
while (!pq.isEmpty() && pq.peek()[1] < q[0]) {
125+
pq.poll();
126+
}
127+
if (!pq.isEmpty()) {
128+
ans[q[1]] = pq.peek()[0];
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) {
142+
int n = intervals.size(), m = queries.size();
143+
sort(intervals.begin(), intervals.end());
144+
using pii = pair<int, int>;
145+
vector<pii> qs;
146+
for (int i = 0; i < m; ++i) {
147+
qs.emplace_back(queries[i], i);
148+
}
149+
sort(qs.begin(), qs.end());
150+
vector<int> ans(m, -1);
151+
priority_queue<pii, vector<pii>, greater<pii>> pq;
152+
int i = 0;
153+
for (auto& [x, j] : qs) {
154+
while (i < n && intervals[i][0] <= x) {
155+
int a = intervals[i][0], b = intervals[i][1];
156+
pq.emplace(b - a + 1, b);
157+
++i;
158+
}
159+
while (!pq.empty() && pq.top().second < x) {
160+
pq.pop();
161+
}
162+
if (!pq.empty()) {
163+
ans[j] = pq.top().first;
164+
}
165+
}
166+
return ans;
167+
}
168+
};
169+
```
72170
171+
### **Go**
172+
173+
```go
174+
func minInterval(intervals [][]int, queries []int) []int {
175+
n, m := len(intervals), len(queries)
176+
sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
177+
qs := make([][2]int, m)
178+
ans := make([]int, m)
179+
for i := range qs {
180+
qs[i] = [2]int{queries[i], i}
181+
ans[i] = -1
182+
}
183+
sort.Slice(qs, func(i, j int) bool { return qs[i][0] < qs[j][0] })
184+
pq := hp{}
185+
i := 0
186+
for _, q := range qs {
187+
x, j := q[0], q[1]
188+
for i < n && intervals[i][0] <= x {
189+
a, b := intervals[i][0], intervals[i][1]
190+
heap.Push(&pq, pair{b - a + 1, b})
191+
i++
192+
}
193+
for len(pq) > 0 && pq[0].r < x {
194+
heap.Pop(&pq)
195+
}
196+
if len(pq) > 0 {
197+
ans[j] = pq[0].v
198+
}
199+
}
200+
return ans
201+
}
202+
203+
type pair struct{ v, r int }
204+
type hp []pair
205+
206+
func (h hp) Len() int { return len(h) }
207+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
208+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
209+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
210+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
73211
```
74212

75213
### **...**

solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,135 @@
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def minInterval(self, intervals: List[List[int]], queries: List[int]) -> List[int]:
58+
n, m = len(intervals), len(queries)
59+
intervals.sort()
60+
queries = sorted((x, i) for i, x in enumerate(queries))
61+
ans = [-1] * m
62+
pq = []
63+
i = 0
64+
for x, j in queries:
65+
while i < n and intervals[i][0] <= x:
66+
a, b = intervals[i]
67+
heappush(pq, (b - a + 1, b))
68+
i += 1
69+
while pq and pq[0][1] < x:
70+
heappop(pq)
71+
if pq:
72+
ans[j] = pq[0][0]
73+
return ans
5774
```
5875

5976
### **Java**
6077

6178
```java
79+
class Solution {
80+
public int[] minInterval(int[][] intervals, int[] queries) {
81+
int n = intervals.length, m = queries.length;
82+
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
83+
int[][] qs = new int[m][0];
84+
for (int i = 0; i < m; ++i) {
85+
qs[i] = new int[] {queries[i], i};
86+
}
87+
Arrays.sort(qs, (a, b) -> a[0] - b[0]);
88+
int[] ans = new int[m];
89+
Arrays.fill(ans, -1);
90+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
91+
int i = 0;
92+
for (int[] q : qs) {
93+
while (i < n && intervals[i][0] <= q[0]) {
94+
int a = intervals[i][0], b = intervals[i][1];
95+
pq.offer(new int[] {b - a + 1, b});
96+
++i;
97+
}
98+
while (!pq.isEmpty() && pq.peek()[1] < q[0]) {
99+
pq.poll();
100+
}
101+
if (!pq.isEmpty()) {
102+
ans[q[1]] = pq.peek()[0];
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) {
116+
int n = intervals.size(), m = queries.size();
117+
sort(intervals.begin(), intervals.end());
118+
using pii = pair<int, int>;
119+
vector<pii> qs;
120+
for (int i = 0; i < m; ++i) {
121+
qs.emplace_back(queries[i], i);
122+
}
123+
sort(qs.begin(), qs.end());
124+
vector<int> ans(m, -1);
125+
priority_queue<pii, vector<pii>, greater<pii>> pq;
126+
int i = 0;
127+
for (auto& [x, j] : qs) {
128+
while (i < n && intervals[i][0] <= x) {
129+
int a = intervals[i][0], b = intervals[i][1];
130+
pq.emplace(b - a + 1, b);
131+
++i;
132+
}
133+
while (!pq.empty() && pq.top().second < x) {
134+
pq.pop();
135+
}
136+
if (!pq.empty()) {
137+
ans[j] = pq.top().first;
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
62144
145+
### **Go**
146+
147+
```go
148+
func minInterval(intervals [][]int, queries []int) []int {
149+
n, m := len(intervals), len(queries)
150+
sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
151+
qs := make([][2]int, m)
152+
ans := make([]int, m)
153+
for i := range qs {
154+
qs[i] = [2]int{queries[i], i}
155+
ans[i] = -1
156+
}
157+
sort.Slice(qs, func(i, j int) bool { return qs[i][0] < qs[j][0] })
158+
pq := hp{}
159+
i := 0
160+
for _, q := range qs {
161+
x, j := q[0], q[1]
162+
for i < n && intervals[i][0] <= x {
163+
a, b := intervals[i][0], intervals[i][1]
164+
heap.Push(&pq, pair{b - a + 1, b})
165+
i++
166+
}
167+
for len(pq) > 0 && pq[0].r < x {
168+
heap.Pop(&pq)
169+
}
170+
if len(pq) > 0 {
171+
ans[j] = pq[0].v
172+
}
173+
}
174+
return ans
175+
}
176+
177+
type pair struct{ v, r int }
178+
type hp []pair
179+
180+
func (h hp) Len() int { return len(h) }
181+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
182+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
183+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
184+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
63185
```
64186

65187
### **...**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<int> minInterval(vector<vector<int>>& intervals, vector<int>& queries) {
4+
int n = intervals.size(), m = queries.size();
5+
sort(intervals.begin(), intervals.end());
6+
using pii = pair<int, int>;
7+
vector<pii> qs;
8+
for (int i = 0; i < m; ++i) {
9+
qs.emplace_back(queries[i], i);
10+
}
11+
sort(qs.begin(), qs.end());
12+
vector<int> ans(m, -1);
13+
priority_queue<pii, vector<pii>, greater<pii>> pq;
14+
int i = 0;
15+
for (auto& [x, j] : qs) {
16+
while (i < n && intervals[i][0] <= x) {
17+
int a = intervals[i][0], b = intervals[i][1];
18+
pq.emplace(b - a + 1, b);
19+
++i;
20+
}
21+
while (!pq.empty() && pq.top().second < x) {
22+
pq.pop();
23+
}
24+
if (!pq.empty()) {
25+
ans[j] = pq.top().first;
26+
}
27+
}
28+
return ans;
29+
}
30+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func minInterval(intervals [][]int, queries []int) []int {
2+
n, m := len(intervals), len(queries)
3+
sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })
4+
qs := make([][2]int, m)
5+
ans := make([]int, m)
6+
for i := range qs {
7+
qs[i] = [2]int{queries[i], i}
8+
ans[i] = -1
9+
}
10+
sort.Slice(qs, func(i, j int) bool { return qs[i][0] < qs[j][0] })
11+
pq := hp{}
12+
i := 0
13+
for _, q := range qs {
14+
x, j := q[0], q[1]
15+
for i < n && intervals[i][0] <= x {
16+
a, b := intervals[i][0], intervals[i][1]
17+
heap.Push(&pq, pair{b - a + 1, b})
18+
i++
19+
}
20+
for len(pq) > 0 && pq[0].r < x {
21+
heap.Pop(&pq)
22+
}
23+
if len(pq) > 0 {
24+
ans[j] = pq[0].v
25+
}
26+
}
27+
return ans
28+
}
29+
30+
type pair struct{ v, r int }
31+
type hp []pair
32+
33+
func (h hp) Len() int { return len(h) }
34+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
35+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
36+
func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
37+
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }

0 commit comments

Comments
 (0)