Skip to content

K Closest Points to the Origin

MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of points points, where each point is [x, y], and a number k, return the k points closest to the origin (0, 0) by straight-line distance. You can return them in any order.

points = [[1, 3], [-2, 2]],          k = 1  →  [[-2, 2]]
points = [[3, 3], [5, -1], [-2, 4]], k = 2  →  [[3, 3], [-2, 4]]

Constraints

  • 1 <= k <= points.length <= 10^4
  • -10^4 <= x, y <= 10^4
  • The tests are chosen so the answer is unique.