Advertisement

K Closest Points to Origin - LeetCode 973 Solution

K Closest Points to Origin - Complete Solution Guide

K Closest Points to Origin is LeetCode problem 973, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of points where points[i] = [x i , y i ] 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., √(x 1 - x 2 ) 2 + (y 1 - y 2 ) 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 betwe

Detailed Explanation

The problem asks us to find the 'k' points in a given array of points that are closest to the origin (0, 0) in a 2D plane. The distance between two points is calculated using the Euclidean distance formula. The output should be a list containing these 'k' closest points. The order of the points in the output doesn't matter, and the solution is guaranteed to be unique (except for the order).

Solution Approach

The provided solution utilizes a max-heap (priority queue) to keep track of the 'k' closest points encountered so far. The algorithm iterates through each point in the input array. For each point, it calculates the squared Euclidean distance from the origin. If the heap has fewer than 'k' points, the current point is added to the heap. Otherwise, if the current point's squared distance is less than the squared distance of the farthest point currently in the heap (the root of the max-heap), the farthest point is removed, and the current point is added. After processing all points, the heap contains the 'k' closest points to the origin, which are then returned as the result.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty max-heap (priority queue) to store the 'k' closest points and their distances.
  2. Step 2: Iterate through the input array of points.
  3. Step 3: For each point, calculate its squared Euclidean distance from the origin.
  4. Step 4: If the size of the heap is less than 'k', add the point and its squared distance (negated) to the heap. Negation ensures it behaves as max-heap for smallest distances.
  5. Step 5: If the size of the heap is equal to 'k', compare the squared distance of the current point with the largest squared distance in the heap (the root element).
  6. Step 6: If the current point's squared distance is smaller than the largest squared distance in the heap, remove the root of the heap and add the current point and its (negated) squared distance to the heap.
  7. Step 7: After iterating through all the points, the heap will contain the 'k' closest points. Extract these points from the heap and return them as the result.

Key Insights

  • Insight 1: The Euclidean distance calculation involves a square root, but since we only need to compare distances, we can optimize by comparing the squared distances. This avoids the computationally expensive square root operation.
  • Insight 2: A max-heap (priority queue) is well-suited for this problem. It allows us to efficiently keep track of the 'k' closest points seen so far. The max-heap stores the distances, always maintaining the largest distance among the 'k' closest points at the top.
  • Insight 3: We can iterate through the points. If the current point's distance is less than the maximum distance currently in our max-heap, we can replace the farthest point in the max-heap with the current point.

Complexity Analysis

Time Complexity: O(nlogk)

Space Complexity: O(k)

Topics

This problem involves: Array, Math, Divide and Conquer, Geometry, Sorting, Heap (Priority Queue), Quickselect.

Companies

Asked at: Asana, Axon, Snap, Swiggy, Visa, Wix.