Skip to content

K Closest Points to Origin

MediumPython 3HeapsArraysGeometry

You are given a list 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), measured by Euclidean distance. The answer is guaranteed to be unique (no tie in distance straddles the k-th place); the returned points may be in any order.

Constraints:
  - 1 <= k <= len(points) <= 10^4
  - -10^4 <= x, y <= 10^4
Examples:
  points = [[1, 3], [-2, 2]], k = 1          -> [[-2, 2]]
  points = [[3, 3], [5, -1], [-2, 4]], k = 2 -> [[3, 3], [-2, 4]] (any order)

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…