Skip to content

Commit 3bb5392

Browse files
committed
K Closest Points of Origin
1 parent 03ad0b2 commit 3bb5392

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

0973_kClosestPointsToOrigin.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const distance = point => point[0] ** 2 + point[1] ** 2;
2+
3+
/**
4+
* @param {number[][]} points Array of points on a plane.
5+
* @param {number} k How many closes points to 0,0 to return.
6+
* @return {number[][]} Array of k closes points to 0,0.
7+
* @summary K Closest Points to Origin {@link https://leetcode.com/problems/k-closest-points-to-origin/}
8+
* @description Given list of points, return k closest points to 0,0 (Euclidean distance).
9+
* Space O(n) - create array of length k.
10+
* Time O(nlogn) - logn for js built in sorting, n comparison operations(?).
11+
*/
12+
const kClosest = (points, k) => points.sort((a, b) => distance(a) - distance(b)).slice(0, k);

0 commit comments

Comments
 (0)