K Closest Points to Origin
Return k points with smallest squared Euclidean distances from the origin. Result order is unrestricted.
Example 1
points = [[1,2],[4,4],[0,1]]
k = 2[[1,2],[0,1]]Explanation
Squared distances are 5,32,1; select [1,2] and [0,1].
Example 2
points = [[3,0]]
k = 1[[3,0]]Explanation
Requesting the sole point returns it.
Example 3
points = [[-1,-1],[2,0]]
k = 1[[-1,-1]]Explanation
Squared distances are 2 and 4; negative coordinates still contribute positive squares.
Constraints
- 1 <= k <= number of points <= 10000
- Coordinates are -10000..10000
- The distance boundary at k has no tie
Function contract
kClosestArguments: json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[[1,2],[4,4],[0,1]]2