-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFind K Pairs with Smallest Sums.js
More file actions
67 lines (54 loc) · 1.65 KB
/
Find K Pairs with Smallest Sums.js
File metadata and controls
67 lines (54 loc) · 1.65 KB
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
57
58
59
60
61
62
63
64
65
66
67
var kSmallestPairs = function(nums1, nums2, k) {
if (nums1.length === 0 || nums2.length === 0 || k === 0) {
return [];
}
const heap = [];
for (let i = 0; i < Math.min(nums1.length, k); i++) {
heap.push([nums1[i] + nums2[0], i, 0]);
}
const swap = (a, b) => {
[heap[a], heap[b]] = [heap[b], heap[a]];
};
const heapifyUp = (index) => {
while (index > 0) {
let parent = Math.floor((index - 1) / 2);
if (heap[parent][0] <= heap[index][0]) break;
swap(parent, index);
index = parent;
}
};
const heapifyDown = (index) => {
let size = heap.length;
while (true) {
let smallest = index;
let left = 2 * index + 1;
let right = 2 * index + 2;
if (left < size && heap[left][0] < heap[smallest][0]) {
smallest = left;
}
if (right < size && heap[right][0] < heap[smallest][0]) {
smallest = right;
}
if (smallest === index) break;
swap(index, smallest);
index = smallest;
}
};
for (let i = Math.floor(heap.length / 2); i >= 0; i--) {
heapifyDown(i);
}
const result = [];
while (k > 0 && heap.length > 0) {
let [sum, i, j] = heap[0];
result.push([nums1[i], nums2[j]]);
heap[0] = heap[heap.length - 1];
heap.pop();
heapifyDown(0);
if (j + 1 < nums2.length) {
heap.push([nums1[i] + nums2[j + 1], i, j + 1]);
heapifyUp(heap.length - 1);
}
k--;
}
return result;
};