forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0973-k-closest-points-to-origin.js
181 lines (159 loc) · 5.45 KB
/
0973-k-closest-points-to-origin.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//////////////////////////////////////////////////////////////////////////////
// Sort with Custom Comparator
// Time: O(nlogn)
// Space: O(n)
//////////////////////////////////////////////////////////////////////////////
/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/
var kClosest = function (points, k) {
// Sort the array with a custom lambda comparator function
points.sort((a, b) => squaredDistance(a) - squaredDistance(b));
// Return the first k elements of the sorted array
return points.slice(0, k);
};
// Calculate and return the squared Euclidean distance
const squaredDistance = ([x, y]) => x ** 2 + y ** 2;
//////////////////////////////////////////////////////////////////////////////
// Max Heap or Max Priority Queue
// Time: O(nlogk)
// Space: O(k)
//////////////////////////////////////////////////////////////////////////////
/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/
var kClosest = function (points, k) {
let maxPQ = new MaxPriorityQueue();
for (let point of points) {
let dist = squaredDistance(point);
if (maxPQ.size() < k) {
// Fill the max PQ up to k points
maxPQ.enqueue(point, dist);
} else if (dist < maxPQ.front().priority) {
// If the max PQ is full and a closer point is found,
// discard the farthest point and add this one
maxPQ.dequeue();
maxPQ.enqueue(point, dist);
}
}
// Return all points stored in the max PQ
return maxPQ.toArray().map((el) => el.element);
};
// Calculate and return the squared Euclidean distance
const squaredDistance = ([x, y]) => x ** 2 + y ** 2;
//////////////////////////////////////////////////////////////////////////////
// Binary Search
// Time: O(n)
// Space: O(n)
//////////////////////////////////////////////////////////////////////////////
/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/
var kClosest = function (points, k) {
// Precompute the Euclidean distance for each point
let distances = points.map(euclideanDistance);
// Create a reference array of point indices
let remaining = points.map((_, i) => i);
// Define the initial binary search range
let low = 0,
high = Math.max(...distances);
// Perform a binary search of the distances
// to find the k closest points
let closest = [];
while (k) {
let mid = low + (high - low) / 2;
let [closer, farther] = splitDistances(remaining, distances, mid);
if (closer.length > k) {
// If more than k points are in the closer distances
// then discard the farther points and continue
remaining = closer;
high = mid;
} else {
// Add the closer points to the answer array and keep
// searching the farther distances for the remaining points
k -= closer.length;
closest.push(...closer);
remaining = farther;
low = mid;
}
}
// Return the k closest points using the reference indices
return closest.map((i) => points[i]);
};
var splitDistances = function (remaining, distances, mid) {
// Split the distances around the midpoint
// and return them in separate arrays
let closer = [],
farther = [];
for (let index of remaining) {
if (distances[index] <= mid) {
closer.push(index);
} else {
farther.push(index);
}
}
return [closer, farther];
};
// Calculate and return the squared Euclidean distance
const euclideanDistance = ([x, y]) => x ** 2 + y ** 2;
//////////////////////////////////////////////////////////////////////////////
// QuickSelect
// Time: O(n)
// Space: O(1)
//////////////////////////////////////////////////////////////////////////////
/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/
var kClosest = function (points, k) {
return quickSelect(points, k);
};
var quickSelect = function (points, k) {
let left = 0,
right = points.length - 1;
let pivotIndex = points.length;
while (pivotIndex !== k) {
// Repeatedly partition the array
// while narrowing in on the kth element
pivotIndex = partition(points, left, right);
if (pivotIndex < k) {
left = pivotIndex;
} else {
right = pivotIndex - 1;
}
}
// Return the first k elements of the partially sorted array
return points.slice(0, k);
};
var partition = function (points, left, right) {
let pivot = choosePivot(points, left, right);
let pivotDist = squaredDistance(pivot);
while (left < right) {
// Iterate through the range and swap elements to make sure
// that all points closer than the pivot are to the left
if (squaredDistance(points[left]) >= pivotDist) {
[points[left], points[right]] = [points[right], points[left]];
right--;
} else {
left++;
}
}
// Ensure the left pointer is just past the end of
// the left range then return it as the new pivotIndex
if (squaredDistance(points[left]) < pivotDist) {
left++;
}
return left;
};
// Choose a pivot element of the array
const choosePivot = (points, left, right) =>
points[left + ((right - left) >> 1)];
// Calculate and return the squared Euclidean distance
const squaredDistance = ([x, y]) => x ** 2 + y ** 2;