Skip to content
BASA DSA

K Closest Points to Origin

MediumHeap / Priority Queue

Return k points with smallest squared Euclidean distances from the origin. Result order is unrestricted.

Example 1

Input
points = [[1,2],[4,4],[0,1]]
k = 2
Output
[[1,2],[0,1]]

Explanation

Squared distances are 5,32,1; select [1,2] and [0,1].

Example 2

Input
points = [[3,0]]
k = 1
Output
[[3,0]]

Explanation

Requesting the sole point returns it.

Example 3

Input
points = [[-1,-1],[2,0]]
k = 1
Output
[[-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 contractkClosestArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…