Skip to content

Commit 26650aa

Browse files
authored
feat: add solutions to lc problem: No.0632 (#3672)
1 parent c37ef9b commit 26650aa

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed

solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md

+77-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<pre>
3333
<strong>输入:</strong>nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
3434
<strong>输出:</strong>[20,24]
35-
<strong>解释:</strong>
35+
<strong>解释:</strong>
3636
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
3737
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
3838
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
@@ -269,4 +269,80 @@ impl Solution {
269269

270270
<!-- solution:end -->
271271

272+
<!-- solution:start -->
273+
274+
### 方法二:优先队列(小根堆)
275+
276+
<!-- tabs:start -->
277+
278+
#### TypeScript
279+
280+
```ts
281+
const smallestRange = (nums: number[][]): number[] => {
282+
const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
283+
const n = nums.length;
284+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
285+
286+
for (let j = 0; j < n; j++) {
287+
const x = nums[j][0];
288+
pq.enqueue([x, j, 0]);
289+
max = Math.max(max, x);
290+
}
291+
292+
while (pq.size() === n) {
293+
const [min, j, i] = pq.dequeue().element;
294+
295+
if (max - min < r - l) {
296+
[l, r] = [min, max];
297+
}
298+
299+
const iNext = i + 1;
300+
if (iNext < nums[j].length) {
301+
const next = nums[j][iNext];
302+
pq.enqueue([next, j, iNext]);
303+
max = Math.max(max, next);
304+
}
305+
}
306+
307+
return [l, r];
308+
};
309+
```
310+
311+
#### JavaScript
312+
313+
```js
314+
const smallestRange = nums => {
315+
const pq = new MinPriorityQueue({ priority: ([x]) => x });
316+
const n = nums.length;
317+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
318+
319+
for (let j = 0; j < n; j++) {
320+
const x = nums[j][0];
321+
pq.enqueue([x, j, 0]);
322+
max = Math.max(max, x);
323+
}
324+
325+
while (pq.size() === n) {
326+
const [min, j, i] = pq.dequeue().element;
327+
328+
if (max - min < r - l) {
329+
[l, r] = [min, max];
330+
}
331+
332+
const iNext = i + 1;
333+
if (iNext < nums[j].length) {
334+
const next = nums[j][iNext];
335+
pq.enqueue([next, j, iNext]);
336+
max = Math.max(max, next);
337+
}
338+
}
339+
340+
return [l, r];
341+
};
342+
```
343+
344+
<!-- tabs:end -->
345+
346+
<!-- solution:end -->
347+
272348
<!-- problem:end -->

solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md

+76
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,80 @@ impl Solution {
257257

258258
<!-- solution:end -->
259259

260+
<!-- solution:start -->
261+
262+
### Solution 2: Priority Queue (Heap)
263+
264+
<!-- tabs:start -->
265+
266+
#### TypeScript
267+
268+
```ts
269+
const smallestRange = (nums: number[][]): number[] => {
270+
const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
271+
const n = nums.length;
272+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
273+
274+
for (let j = 0; j < n; j++) {
275+
const x = nums[j][0];
276+
pq.enqueue([x, j, 0]);
277+
max = Math.max(max, x);
278+
}
279+
280+
while (pq.size() === n) {
281+
const [min, j, i] = pq.dequeue().element;
282+
283+
if (max - min < r - l) {
284+
[l, r] = [min, max];
285+
}
286+
287+
const iNext = i + 1;
288+
if (iNext < nums[j].length) {
289+
const next = nums[j][iNext];
290+
pq.enqueue([next, j, iNext]);
291+
max = Math.max(max, next);
292+
}
293+
}
294+
295+
return [l, r];
296+
};
297+
```
298+
299+
#### JavaScript
300+
301+
```js
302+
const smallestRange = nums => {
303+
const pq = new MinPriorityQueue({ priority: ([x]) => x });
304+
const n = nums.length;
305+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
306+
307+
for (let j = 0; j < n; j++) {
308+
const x = nums[j][0];
309+
pq.enqueue([x, j, 0]);
310+
max = Math.max(max, x);
311+
}
312+
313+
while (pq.size() === n) {
314+
const [min, j, i] = pq.dequeue().element;
315+
316+
if (max - min < r - l) {
317+
[l, r] = [min, max];
318+
}
319+
320+
const iNext = i + 1;
321+
if (iNext < nums[j].length) {
322+
const next = nums[j][iNext];
323+
pq.enqueue([next, j, iNext]);
324+
max = Math.max(max, next);
325+
}
326+
}
327+
328+
return [l, r];
329+
};
330+
```
331+
332+
<!-- tabs:end -->
333+
334+
<!-- solution:end -->
335+
260336
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const smallestRange = nums => {
2+
const pq = new MinPriorityQueue({ priority: ([x]) => x });
3+
const n = nums.length;
4+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
5+
6+
for (let j = 0; j < n; j++) {
7+
const x = nums[j][0];
8+
pq.enqueue([x, j, 0]);
9+
max = Math.max(max, x);
10+
}
11+
12+
while (pq.size() === n) {
13+
const [min, j, i] = pq.dequeue().element;
14+
15+
if (max - min < r - l) {
16+
[l, r] = [min, max];
17+
}
18+
19+
const iNext = i + 1;
20+
if (iNext < nums[j].length) {
21+
const next = nums[j][iNext];
22+
pq.enqueue([next, j, iNext]);
23+
max = Math.max(max, next);
24+
}
25+
}
26+
27+
return [l, r];
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const smallestRange = (nums: number[][]): number[] => {
2+
const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
3+
const n = nums.length;
4+
let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
5+
6+
for (let j = 0; j < n; j++) {
7+
const x = nums[j][0];
8+
pq.enqueue([x, j, 0]);
9+
max = Math.max(max, x);
10+
}
11+
12+
while (pq.size() === n) {
13+
const [min, j, i] = pq.dequeue().element;
14+
15+
if (max - min < r - l) {
16+
[l, r] = [min, max];
17+
}
18+
19+
const iNext = i + 1;
20+
if (iNext < nums[j].length) {
21+
const next = nums[j][iNext];
22+
pq.enqueue([next, j, iNext]);
23+
max = Math.max(max, next);
24+
}
25+
}
26+
27+
return [l, r];
28+
};

0 commit comments

Comments
 (0)