-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-k-closest-elements.js
More file actions
53 lines (46 loc) · 1.37 KB
/
find-k-closest-elements.js
File metadata and controls
53 lines (46 loc) · 1.37 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
/**
* @param {number[]} arr
* @param {number} k
* @param {number} x
* @return {number[]}
*/
var findClosestElements = function (arr, k, x) {
let left = 0;
let right = arr.length - 1;
// Use binary search to narrow the range to k elements
while (right - left >= k) {
if (Math.abs(arr[left] - x) > Math.abs(arr[right] - x)) {
left++; // Remove the element farther from x on the left
} else {
right--; // Remove the element farther from x on the right
}
}
// Return the k closest elements, which are guaranteed to be in the range [left, right]
return arr.slice(left, left + k);
};
/**
* @param {number[]} arr
* @param {number} k
* @param {number} x
* @return {number[]}
*/
var findClosestElements = function (arr, k, x) {
// Use a max-heap to store the closest elements
const maxHeap = [];
const pushToHeap = (dist, val) => {
// Add the pair (distance, value) to the heap
maxHeap.push([dist, val]);
maxHeap.sort((a, b) => b[0] - a[0]); // Sort by descending distance
};
for (let num of arr) {
const distance = Math.abs(num - x);
pushToHeap(distance, num);
// If the heap size exceeds k, remove the farthest element
if (maxHeap.length > k) {
maxHeap.pop();
}
}
// Extract the elements from the heap and sort them
const result = maxHeap.map(([_, val]) => val);
return result.sort((a, b) => a - b);
};