K Closest Points to Origin
Problem
You are given a list of points on a 2D plane and an integer k. Return the k points closest to the origin (0, 0), measured by straight-line distance. Any order of the answer is acceptable.
Example. With points [[1, 3], [-2, 2]] and k = 1, the distances from the origin are roughly 3.16 and 2.83, so the closer point [-2, 2] is the answer.
Key idea
Sorting every point by distance and taking the first k works, but it pays for a full ordering of all n points when only the k smallest distances actually matter. That extra work can be avoided by keeping just the k best candidates seen so far instead of ranking everything.
Maintain a max-heap capped at size k, ordered by squared distance from the origin to avoid unnecessary square roots. Process each point: add it to the heap, and if the heap now exceeds k elements, remove the one with the largest distance, which sits at the top. After processing every point, the heap contains exactly the k closest, because any point farther than the current worst of the top k gets evicted as soon as a closer candidate displaces it.
An alternative for very large inputs is a quickselect-style partition on distance, which can average better than the heap's O(n log k) but has worse worst-case behavior and no streaming-friendly property.
Solution
Complexity
- Time: O(n log k). Each of the n points does an O(log k) heap operation.
- Space: O(k). The heap holds at most k points; O(1) extra if the answer can overwrite the input.
Watch out for
- Compare squared distances, not raw coordinates or distances needing a square root on every comparison; it is faster and avoids floating-point noise.
- Use a max-heap so the worst of the current top k is the one evicted, not a min-heap, which would evict the best.
Pattern
This is another instance of the bounded top-k heap pattern: keep only the k best candidates by a comparable key and let the heap evict the worst as better ones arrive, the same technique used for streaming kth-largest problems.