Problem Overview
You are given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, and an integer k.
Your task is to return the k points closest to the origin (0, 0).
The distance between any point (x, y) and the origin (0, 0) is calculated as:
distance = √(x² + y²)
However, since we only need to compare distances, we can use the squared distance x² + y² to avoid unnecessary square root calculations.
Example 1:
Input:points = [[1,3], [-2,2]], k = 1
Output:[[-2,2]]
Explanation:
The distance of each point from the origin is:
[1,3] → √(1² + 3²) = √10[-2,2] → √(4 + 4) = √8
Since √8 < √10, the closest point is [-2, 2].
Example 2:
Input:points = [[3,3],[5,-1],[-2,4]], k = 2
Output:[[3,3],[-2,4]]
Explanation:
The distances are:
[3,3] → 18[5,-1] → 26[-2,4] → 20
The two smallest distances are 18 and 20, corresponding to [3,3] and [-2,4].
Intuition / Approach
The problem asks for the k smallest distances from the origin among the given points. This can be viewed as a partial sorting problem.
There are several possible approaches:
- Sort All Points by Distance:
- Compute the distance for each point.
- Sort all points based on their squared distance.
- Pick the first
kpoints.
This is simple but less efficient for large datasets, as sorting takesO(n log n)time.
- Use a Max-Heap of Size K:
- Maintain a heap (priority queue) that stores the
kclosest points seen so far. - For each new point, calculate its distance:
- If the heap has fewer than
kpoints, add it. - Otherwise, compare the distance of the new point with the maximum distance in the heap:
- If smaller, remove the farthest point and add the new one.
- If the heap has fewer than
- This ensures the heap always contains the
kclosest points.
This approach is efficient whennis large andkis much smaller thann.
- Maintain a heap (priority queue) that stores the
We will use the max-heap approach, as it’s more scalable and optimal in most practical scenarios.
Algorithm Explanation
- Create a priority queue (max-heap) where the point with the largest distance is at the top.
- Iterate over all points:
- Compute the squared distance for each point as
x² + y². - Push the point and its distance into the heap.
- If the heap size exceeds
k, remove the farthest point.
- Compute the squared distance for each point as
- Once all points are processed, the heap contains the
kclosest points. - Extract and return these
kpoints.
Java Code Implementation
import java.util.*;
public class KClosestPointsToOrigin {
public int[][] kClosest(int[][] points, int k) {
// Step 1: Create a max-heap based on squared distance
PriorityQueue<int[]> maxHeap = new PriorityQueue<>(
(a, b) -> Integer.compare(distance(b), distance(a))
);
// Step 2: Process each point
for (int[] point : points) {
maxHeap.offer(point); // Add point to heap
if (maxHeap.size() > k) {
maxHeap.poll(); // Remove the farthest point
}
}
// Step 3: Extract k closest points
int[][] result = new int[k][2];
for (int i = 0; i < k; i++) {
result[i] = maxHeap.poll();
}
return result;
}
// Helper method to calculate squared distance from origin
private int distance(int[] point) {
return point[0] * point[0] + point[1] * point[1];
}
// Optional main method for testing
public static void main(String[] args) {
KClosestPointsToOrigin obj = new KClosestPointsToOrigin();
int[][] points1 = {{1,3}, {-2,2}};
int k1 = 1;
System.out.println(Arrays.deepToString(obj.kClosest(points1, k1))); // [[-2, 2]]
int[][] points2 = {{3,3}, {5,-1}, {-2,4}};
int k2 = 2;
System.out.println(Arrays.deepToString(obj.kClosest(points2, k2))); // [[3, 3], [-2, 4]]
}
}
Dry Run Example
Input:points = [[3,3], [5,-1], [-2,4]], k = 2
Step-by-step:
- Start with an empty heap.
- Add
[3,3] → distance = 18. Heap =[[3,3]] - Add
[5,-1] → distance = 26. Heap =[[5,-1], [3,3]] - Add
[-2,4] → distance = 20. Heap =[[5,-1], [3,3], [-2,4]]
Heap size exceedsk = 2, remove the farthest →[5,-1](distance 26).
Remaining heap =[[3,3], [-2,4]] - Extract heap contents →
[[3,3], [-2,4]]
Output: [[3,3], [-2,4]]
Complexity Analysis
- Time Complexity:
O(n log k)- Each point insertion/removal in the heap takes
O(log k)time. - For
npoints, total operations =O(n log k).
- Each point insertion/removal in the heap takes
- Space Complexity:
O(k)
The heap stores at mostkpoints at any time.
If you use the sorting-based approach, the time complexity would be O(n log n) and space complexity O(n).
Alternative Approaches
- Sorting-Based Approach:
- Compute all distances and sort all points.
- Return the first
kpoints. - Simpler to implement but slower for large
n.
Time complexity:O(n log n).
- QuickSelect Algorithm (Average O(n)):
- Use QuickSelect (similar to quicksort partitioning) to find the
kth smallest distance. - Partition points so that all smaller distances come before the
kth distance. - Faster average case but complex to implement and less stable in worst-case scenarios.
- Use QuickSelect (similar to quicksort partitioning) to find the
