You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Given `M` sorted arrays, find the smallest range that includes at least one number from each of the `M` lists.
299
299
300
300
This problem follows the [K-way merge pattern](#pattern-14-k-way-merge) and we can follow a similar approach as discussed in [Merge K Sorted Lists](#👩🏽🦯-🌴-merge-k-sorted-arrays).
301
301
302
-
We can start by inserting the first number from all the arrays in a `min-heap()`. We will keep track of the largest number that we have inserted in the heap (let’s call it `currentMax`).
302
+
We can start by inserting the first number from all the arrays in a `min-heap()`. We will keep track of the largest number that we have inserted in the heap (let’s call it `maxNumber`).
303
303
304
-
In a loop, we’ll take the smallest (top) element from the `min-heap()` and `currentMax` has the largest element that we inserted in the heap. If these two numbers give us a smaller range, we’ll update our range. Finally, if the array of the top element has more elements, we’ll insert the next element to the heap.
304
+
In a loop, we’ll take the smallest (top) element from the `min-heap()` and `maxNumber` has the largest element that we inserted in the heap. If these two numbers give us a smaller range, we’ll update our range. Finally, if the array of the top element has more elements, we’ll insert the next element to the heap.
305
305
306
306
We can finish searching the minimum range as soon as an array is completed or, in other terms, the <b>heap</b> has less than `M` elements.
// The range [4, 7] includes 5 from L1, 4 from L2 and 7 from L3.
367
-
368
-
console.log(`Smallest range is:
369
-
${findSmallestRange([
370
-
[1, 9],
371
-
[4, 12],
372
-
[7, 10, 16],
373
-
])}`);
374
-
// The range [9, 12] includes 9 from L1, 12 from L2 and 10 from L3.
375
-
376
-
console.log(`Smallest range is:
377
-
${findSmallestRange([
414
+
${smallestRange([
378
415
[4, 10, 15, 24, 26],
379
416
[0, 9, 12, 20],
380
417
[5, 18, 22, 30],
@@ -385,14 +422,30 @@ ${findSmallestRange([
385
422
// List 3: [5, 18, 22, 30], 22 is in range [20,24].
386
423
387
424
console.log(`Smallest range is:
388
-
${findSmallestRange(
425
+
${smallestRange(
389
426
(nums = [
390
427
[1, 2, 3],
391
428
[1, 2, 3],
392
429
[1, 2, 3],
393
430
])
394
431
)}`);
395
432
// [1,1]
433
+
434
+
console.log(`Smallest range is:
435
+
${smallestRange([
436
+
[1, 5, 8],
437
+
[4, 12],
438
+
[7, 8, 10],
439
+
])}`);
440
+
// The range [4, 7] includes 5 from L1, 4 from L2 and 7 from L3.
441
+
442
+
console.log(`Smallest range is:
443
+
${smallestRange([
444
+
[1, 9],
445
+
[4, 12],
446
+
[7, 10, 16],
447
+
])}`);
448
+
// The range [9, 12] includes 9 from L1, 12 from L2 and 10 from L3.
396
449
```
397
450
398
451
- Since, at most, we’ll be going through all the elements of all the arrays and will remove/add one element in the heap in each step, the time complexity of the above algorithm will be `O(N*logM)` where `N` is the total number of elements in all the `M` input arrays.
0 commit comments