← All problems

K Closest Points to Origin

Medium heapsquickselect LeetCode ↗
Given an array of points in the plane, return the k points closest to the origin (0, 0). Avoid computing all distances and fully sorting when k is much smaller than n.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n log k), Space O(k)

Common pitfalls

  • Computing sqrt unnecessarily
  • Min-heap of all points

Your solution

Log in to save your code and track progress.