-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0632-smallest-range-covering-elements-from-k-lists.js
56 lines (49 loc) · 1.65 KB
/
0632-smallest-range-covering-elements-from-k-lists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* 632. Smallest Range Covering Elements from K Lists
* https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/
* Difficulty: Hard
*
* You have k lists of sorted integers in non-decreasing order. Find the smallest range that
* includes at least one number from each of the k lists.
*
* We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c
* if b - a == d - c.
*/
/**
* @param {number[][]} nums
* @return {number[]}
*/
var smallestRange = function(nums) {
const k = nums.length;
const listCountMap = new Map();
let coveredLists = 0;
let minDiff = Infinity;
let minStart;
let minEnd;
const allElements = nums.flatMap((list, listIndex) =>
list.map(num => ({ num, index: listIndex }))
).sort((a, b) => a.num - b.num);
let windowStart = 0;
for (let windowEnd = 0; windowEnd < allElements.length; windowEnd++) {
const currentElement = allElements[windowEnd];
const listIndex = currentElement.index;
const count = listCountMap.get(listIndex) ?? 0;
if (count === 0) coveredLists += 1;
listCountMap.set(listIndex, count + 1);
while (coveredLists === k) {
const leftElement = allElements[windowStart];
const range = currentElement.num - leftElement.num;
if (range < minDiff) {
minDiff = range;
minStart = leftElement.num;
minEnd = currentElement.num;
}
const leftListIndex = leftElement.index;
const leftCount = listCountMap.get(leftListIndex);
listCountMap.set(leftListIndex, leftCount - 1);
if (leftCount - 1 === 0) coveredLists -= 1;
windowStart += 1;
}
}
return [minStart, minEnd];
};