Heap pattern
K Closest Points to Origin
Given an array of points on the plane and a value k, return the k points closest to the origin, measured by Euclidean distance. The answer may be returned in any order. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 973
- Medium
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
HeapLeetCode 973
Medium
From the first idea to the best one
- 01 Brute force · sort by distance O(n log n)O(n) space
- 02 Optimized · size-k max-heap O(n log k)O(k) space
What you will learn
- How to read K Closest Points to Origin and spot the heap pattern in it
- Brute force · sort by distance: O(n log n) time, O(n) space
- Optimized · size-k max-heap: O(n log k) time, O(k) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Heap
See K Closest Points to Origin run, step by step
Sign in and this page opens on the animation, with all 7 Heap problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.