diff --git a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md index 205eb1341a136..92b779f1153b2 100644 --- a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md +++ b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md @@ -32,7 +32,7 @@ tags:
输入:nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
输出:[20,24]
-解释:
+解释:
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
@@ -269,4 +269,80 @@ impl Solution {
+
+
+### 方法二:优先队列(小根堆)
+
+
+
+#### TypeScript
+
+```ts
+const smallestRange = (nums: number[][]): number[] => {
+ const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};
+```
+
+#### JavaScript
+
+```js
+const smallestRange = nums => {
+ const pq = new MinPriorityQueue({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};
+```
+
+
+
+
+
diff --git a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md
index 9ef1d2309b078..b465bd5974cfb 100644
--- a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md
+++ b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README_EN.md
@@ -257,4 +257,80 @@ impl Solution {
+
+
+### Solution 2: Priority Queue (Heap)
+
+
+
+#### TypeScript
+
+```ts
+const smallestRange = (nums: number[][]): number[] => {
+ const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};
+```
+
+#### JavaScript
+
+```js
+const smallestRange = nums => {
+ const pq = new MinPriorityQueue({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};
+```
+
+
+
+
+
diff --git a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.js b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.js
new file mode 100644
index 0000000000000..0feed00dc52b9
--- /dev/null
+++ b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.js
@@ -0,0 +1,28 @@
+const smallestRange = nums => {
+ const pq = new MinPriorityQueue({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};
diff --git a/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.ts b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.ts
new file mode 100644
index 0000000000000..55ed4f4420f55
--- /dev/null
+++ b/solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.ts
@@ -0,0 +1,28 @@
+const smallestRange = (nums: number[][]): number[] => {
+ const pq = new MinPriorityQueue<[number, number, number]>({ priority: ([x]) => x });
+ const n = nums.length;
+ let [l, r, max] = [0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
+
+ for (let j = 0; j < n; j++) {
+ const x = nums[j][0];
+ pq.enqueue([x, j, 0]);
+ max = Math.max(max, x);
+ }
+
+ while (pq.size() === n) {
+ const [min, j, i] = pq.dequeue().element;
+
+ if (max - min < r - l) {
+ [l, r] = [min, max];
+ }
+
+ const iNext = i + 1;
+ if (iNext < nums[j].length) {
+ const next = nums[j][iNext];
+ pq.enqueue([next, j, iNext]);
+ max = Math.max(max, next);
+ }
+ }
+
+ return [l, r];
+};