MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / kClosest

Function kClosest

javascript/0973-k-closest-points-to-origin.js:12–18  ·  view source on GitHub ↗
(points, k)

Source from the content-addressed store, hash-verified

10 * @return {number[][]}
11 */
12var kClosest = function (points, k) {
13 // Sort the array with a custom lambda comparator function
14 points.sort((a, b) => squaredDistance(a) - squaredDistance(b));
15
16 // Return the first k elements of the sorted array
17 return points.slice(0, k);
18};
19
20// Calculate and return the squared Euclidean distance
21const squaredDistance = ([x, y]) => x ** 2 + y ** 2;

Callers

nothing calls this directly

Calls 5

squaredDistanceFunction · 0.85
splitDistancesFunction · 0.85
quickSelectFunction · 0.70
sizeMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected