{x}
blog image

K Closest Points to Origin

Given an array of 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 (0, 0).

The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).

 

Example 1:

Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].

Example 2:

Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The answer [[-2,4],[3,3]] would also be accepted.

 

Constraints:

  • 1 <= k <= points.length <= 104
  • -104 <= xi, yi <= 104

Solution Explanation for LeetCode 973: K Closest Points to Origin

This problem asks to find the k points in a given array that are closest to the origin (0, 0). Several approaches can solve this efficiently. We'll analyze three: sorting, using a max-heap, and binary search.

Solution 1: Sorting

This is the most straightforward approach. We calculate the Euclidean distance of each point from the origin using the formula √(x² + y²). Then, we sort the points based on their distances. Finally, we return the first k points from the sorted array.

Code (Python):

import math
 
class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        points.sort(key=lambda p: math.hypot(p[0], p[1])) #Sort based on distance
        return points[:k] #Return first k elements

Time Complexity: O(n log n), dominated by the sorting step. n is the number of points. Space Complexity: O(log n) in most cases due to sorting (in-place merge sort has logarithmic space complexity). Could be O(n) in the worst case for some sorting implementations.

Solution 2: Max Heap (Priority Queue)

A more efficient approach, especially when k is significantly smaller than n, uses a max-heap. We maintain a max-heap of size k. As we iterate through the points, we add each point to the heap. If the heap size exceeds k, we remove the point with the largest distance (the farthest point). At the end, the heap contains the k closest points.

Code (Python):

import heapq
 
class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        heap = []
        for x, y in points:
            dist = -(x**2 + y**2) #Negate to make it a max heap
            heapq.heappush(heap, (dist, [x, y])) #Push distance and point
            if len(heap) > k:
                heapq.heappop(heap) #Remove the farthest if more than k points are present
        return [point for dist, point in heap] #Return the points in the heap

Time Complexity: O(n log k). We iterate through n points, and each heap operation takes O(log k) time. Space Complexity: O(k) to store the heap.

This approach is based on the observation that the distances are sorted implicitly. We can find a critical distance such that all points within this distance include exactly k closest points. We use binary search to efficiently find this critical distance.

Code (Python):

class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        distances = [(x*x + y*y, i) for i, (x, y) in enumerate(points)]
        left, right = 0, max(dist for dist, i in distances) #Find the maximum distance
        while left < right:
            mid = (left + right) // 2
            count = sum(1 for dist, i in distances if dist <= mid)
            if count >= k:
                right = mid
            else:
                left = mid + 1
        return [points[i] for dist, i in distances if dist <= right]

Time Complexity: O(n log M), where M is the maximum distance. The binary search takes O(log M) time, and in each iteration, we count the number of points which take O(n) time. Space Complexity: O(n) to store distances. This approach might not be ideal if the distance range (M) is very large.

Summary:

| Approach | Time Complexity | Space Complexity | Notes | |----------------|-----------------|-----------------|-------------------------------------------------| | Sorting | O(n log n) | O(log n) or O(n) | Simple, but less efficient for large n and small k | | Max Heap | O(n log k) | O(k) | Efficient for large n and small k | | Binary Search | O(n log M) | O(n) | Depends on the range of distances (M) |

The best approach depends on the specific constraints of the input. If k is much smaller than n, the max-heap approach is usually the most efficient. If k is close to n, sorting might be simpler and faster in practice due to the constant factors involved in the heap. Binary search is generally less practical for this problem unless the distances are exceptionally distributed.