K Closest Points to Origin
MediumP2Coding
Given an array of points, where points[i] = [x, y] is a point on the plane, and an integer k, return the k points closest to the origin (0, 0) by Euclidean distance. The answer may be returned in any order. The set of k closest points is guaranteed to be unique (there is no tie at the cut-off).
Example 1
- Input
- points = [[1,3],[-2,2]], k = 1
- Output
- [[-2,2]]
- Why
- Squared distances are 10 and 8.
Example 2
- Input
- points = [[3,3],[5,-1],[-2,4]], k = 2
- Output
- [[3,3],[-2,4]]
Constraints
- 1 <= k <= points.length <= 10^4
- -10^4 <= x, y <= 10^4
- The k closest points are unique.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.