LeetCode 973 ·
Medium
K Closest Points to Origin
Return the k points closest to the origin.
Try it
Step through the core mechanic. The simulator below runs the heap shape this problem is built on.
Walk the pattern
No dedicated step-through for this one yet. The shape is Heap — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.
The approach
Compare by squared distance (no sqrt needed). A size-k max-heap keeps the k closest in O(n log k); quickselect on distance gives expected O(n).
| Aspect | Value |
|---|---|
| Pattern | Heap |
| Recognise it by | K nearest by distance. |
| Time complexity | O(n log k) |
| Space complexity | O(k) |
| Difficulty | Medium |
Who asks it
Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.