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 from the origin to a point (x, y) is the Euclidean distance: sqrt(x^2 + y^2).
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
3 | . (1,3)
2 |
1 | . (3,1) -> not included
--4-3-2-1-0-1-2-3-4--
-1 | . (-2,-2) <- included
-2 |
Input: points = [[1,3],[-2,2]], k = 1 Output: [[-2,2]] Explanation: Distance of (1,3) is sqrt(10). Distance of (-2,2) is sqrt(8). (-2,2) is closer.
Input: points = [[3,3],[5,-1],[-2,4]], k = 2 Output: [[3,3],[-2,4]] Explanation: Distances are sqrt(18), sqrt(26), sqrt(20). The two closest are (3,3) and (-2,4).
1 <= k <= points.length <= 10^4-10^4 <= xi, yi <= 10^4points = [[1, 3], [-2, 2]], k = 1