K Closest Points to Origin - Problem
K Closest Points to Origin
Imagine you're a drone operator standing at the center of a coordinate system (origin at
Given an array
The distance is calculated using the Euclidean distance formula:
Note: You may return the answer in any order. The answer is guaranteed to be unique (except for the order).
Example: If points =
Imagine you're a drone operator standing at the center of a coordinate system (origin at
(0, 0)). You have a list of points scattered across the 2D plane, and you need to identify the K closest points to your position.Given an array
points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin.The distance is calculated using the Euclidean distance formula:
√((x₁ - x₂)² + (y₁ - y₂)²). Since we're measuring from the origin (0, 0), this simplifies to √(x² + y²).Note: You may return the answer in any order. The answer is guaranteed to be unique (except for the order).
Example: If points =
[[1,1],[2,2],[3,3]] and k = 2, return [[1,1],[2,2]] since these are the 2 closest points to origin. Input & Output
example_1.py — Basic Case
$
Input:
points = [[1,1],[2,2],[3,3]], k = 2
›
Output:
[[1,1],[2,2]]
💡 Note:
The distances from origin are: [1,1]→√2≈1.41, [2,2]→√8≈2.83, [3,3]→√18≈4.24. The 2 closest points are [1,1] and [2,2].
example_2.py — Mixed Quadrants
$
Input:
points = [[3,3],[5,-1],[-2,4]], k = 2
›
Output:
[[-2,4],[3,3]]
💡 Note:
Distances: [3,3]→√18≈4.24, [5,-1]→√26≈5.10, [-2,4]→√20≈4.47. The 2 closest are [-2,4] and [3,3]. Order doesn't matter in result.
example_3.py — All Points
$
Input:
points = [[0,1],[1,0]], k = 2
›
Output:
[[0,1],[1,0]]
💡 Note:
Both points have the same distance √1=1 from origin. Since k=2 and we have exactly 2 points, return both. This shows the edge case where k equals the number of points.
Visualization
Tap to expand
Understanding the Visualization
1
Monitor Aircraft Positions
Each aircraft reports its position [x,y]. Calculate distance using d² = x² + y² (skip square root for efficiency).
2
Maintain Priority List
Keep a priority list of K aircraft. Use max-heap so the farthest aircraft in your priority list is always on top.
3
Update Priority Dynamically
When a new aircraft appears, if it's closer than your current farthest priority aircraft, replace the farthest one.
4
Emergency Landing Clearance
Your final priority list contains the K closest aircraft ready for emergency landing clearance.
Key Takeaway
🎯 Key Insight: Max-heap keeps the "worst" of our K best candidates at the top, making it extremely efficient to decide whether to include new candidates or not!
Time & Space Complexity
Time Complexity
O(n log k)
We process n points, and for each point we may do a heap operation (insert/replace) that takes O(log k) time since heap size is at most k.
⚡ Linearithmic
Space Complexity
O(k)
We only store at most k points in the heap at any time, plus O(k) for the result array.
✓ Linear Space
Constraints
- 1 ≤ k ≤ points.length ≤ 104
- -104 ≤ xi, yi ≤ 104
- The answer is guaranteed to be unique (except for the order that it is in)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code