From 2b7a40962c24be94891983dc67a257d1d8d32d7b Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 26 Oct 2024 03:36:28 +0300 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.0632 --- .../README.md | 78 ++++++++++++++++++- .../README_EN.md | 76 ++++++++++++++++++ .../Solution2.js | 28 +++++++ .../Solution2.ts | 28 +++++++ 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.js create mode 100644 solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/Solution2.ts 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..0e59b34511c2d 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 {
 
 
 
+
+
+### 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/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];
+};

From fd7c40e76319ddeb1cc2bdf843343e59ae0bdbcb Mon Sep 17 00:00:00 2001
From: Libin YANG 
Date: Sat, 26 Oct 2024 22:30:09 +0800
Subject: [PATCH 2/2] Update README.md

---
 .../README.md                                                   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 0e59b34511c2d..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	
@@ -271,7 +271,7 @@ impl Solution {
 
 
 
-### Solution 2: Priority Queue (Heap)
+### 方法二:优先队列(小根堆)