LeetCode 973 K Closest Points to Origin - Solution

The core idea

Use a max-heap of size k to track the k closest points seen so far.

Why max-heap? Because you want quick access to the farthest point among your current candidates. When a new point is closer than your farthest candidate, you evict the farthest and add the new point.

Iterate through points:

  • If heap size < k, add the point.
  • If point is closer than heap max, replace the max with this point.

After processing all points, the heap contains the k closest.