K Closest Points
Question (LI.612)
Given a list of points and an origin in a 2D coordinate, find k points that are the closest to the origin.
Example
I: [(4,6), (4,7), (4,4), (2,5), (1,1)], (0,0), 3
O: [(1,1), (2,5), (4,4)]Approach
We want to find the smallest k elements. We want to use a max heap of size k because each pop want to kick out the local max. We want to define a sense of closeness first. A standard Euclidean distance formula will suffice.
Code
Java 8 syntax will be a lot cleaner
// compute the Euclidean distance
private double eucDist (Point origin, Point pt) {
return Math.pow((origin.x - pt.x), 2) + Math.pow((origin.y - pt.y), 2);
}
public Point[] kClosest(Point[] points, Point origin, int k) {
Point[] result = new Point[k];
// create a comparator with distance function
Comparator<Point> PtComp = (Point pt1, Point pt2) -> {
int closeness = (int) (eucDist(origin, pt1) - eucDist(origin, pt2));
if (closeness != 0) return closeness;
else if (pt1.x != pt2.x) return pt1.x - pt2.x;
else return pt1.y - pt2.y;
};
// create a max heap of size k
// either reverseOrder() or flip pt1 and pt2 would work
Queue<Point> maxHeap = new PriorityQueue<>(k, Collections.reverseOrder(PtComp));
// add points to the max heap if bigger then kick off the max
for (Point pt : points) {
if (maxHeap.size() == k) {
if (eucDist(origin, pt) < eucDist(origin, maxHeap.peek())) {
maxHeap.poll();
maxHeap.offer(pt);
}
} else {
maxHeap.offer(pt);
}
}
// max heap to result array (ascending)
int index = k - 1;
while (!maxHeap.isEmpty()) {
result[index--] = maxHeap.poll();
}
return result;
}There is a lot potential for bugs you can get by using a non-native max heap in python. It's conceptualized as a max heap but in reality it is a min heap. It's always a good sanity check go through an example to verify.
Time & Space Complexity
Time O(nlogk) Space O(k)
Memoization
If computing distance is an expensive operation, we can memoization (point, distance) to avoid recomputing the same point.
We used space to save time (a constant factor). Time O(nlogk) Space O(n)
Last updated