K Closest Points to Origin
Given an array points where points[i] = [xi, yi] represents a point on the plane, and an integer k, return the k points closest to the origin (0, 0) measured by Euclidean distance. The answer may be returned in any order and is guaranteed to be unique.
Open official problem prompt ↗Return the k points with the smallest distances to the origin, ignoring their relative order.
Keeping a shortlist of the k nearest friends on a map: whenever you spot someone closer than your current farthest shortlisted friend, drop that farthest one and add the newcomer.
- Input
- points = [[1, 3], [-2, 2]], k = 1
- Output
- [[-2, 2]]
- Why
- Squared distances are 1+9 = 10 and 4+4 = 8; [-2,2] is closer, so it is the single closest point.
1 <= k <= points.length <= 10^4-10^4 <= xi, yi <= 10^4