Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 1.05 KB

K Closest Points to Origin.md

File metadata and controls

25 lines (17 loc) · 1.05 KB

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).

The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).

Screen Shot 2021-09-20 at 12 34 42 AM

Simplest O(nlogn) answer, we sort the array, and then use slice to pick the nearst

/*
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]

After the formular is [18 26 20], sort the order to become [18, 20, 26]
Accordingly, it becomes [[3,3],[-2,4],[5,-1]], pick the first two, the answer is [[3,3],[-2,4]]
*/
var kClosest = function(points, K) {
    points.sort((a,b) => (a[0]*a[0] + a[1]*a[1]) - (b[0]*b[0] + b[1]*b[1]))

    return points.slice(0,K)
};