K Closest Points to Origin Problem

K Closest Points to Origin: An In-Depth Explanation

Finding the closest points to a specific location, such as the origin (0, 0), is a common problem in computational geometry and has a variety of applications in fields like computer vision, geospatial analysis, and clustering.

In this article, we will explore the problem, break it down step-by-step, and discuss an efficient solution.

Problem: 

You are given an 2-D array points where  points[i] = [xi, yi] represents the coordinates of a point on an X-Y axis plane. You are also given an integer k.

Return the k closest points to the origin 

The distance between two points is defined as the Euclidean distance (sqrt((x1 – x2)^2 + (y1 – y2)^2)).

You may return the answer in any order.

Problem Breakdown

You are given:

  1. A list of 2D points, where each point is represented as [xi, yi].
  2. An integer k, which represents the number of closest points to the origin (0, 0) that you need to return.

You need to find the k points that have the smallest distances to the origin. If there are multiple solutions (points with the same distance), any valid output order is acceptable.

Example 1:

Explanation:

 The distance between (0, 2) and the origin (0, 0) is 2. The distance between (2, 2) and the origin is sqrt(2^2 + 2^2) = 2.82842. So the closest point to the origin is (0, 2).

Explanation: The output [2,0],[0,2] would also be accepted.

Constraints:

  • 1 <= k <= points.length <= 1000
  • -100 <= points[i][0], points[i][1] <= 100

Approach

Key Idea: Max-Heap Simulation

To efficiently retrieve the two heaviest stones, we use a max-heap. However, Python’s heapq library provides a min-heap implementation by default, so we simulate a max-heap by storing negative values.

Algorithm

Steps

  1. Initialize the Heap: Convert all stone weights to negative and push them into a heap.
  2. Simulation Loop:
    • Pop the two largest elements (smallest in the min-heap due to negative values).
    • If the stones are not equal, compute the new weight and push it back into the heap.
  3. Result:
    • If the heap is empty, return 0.
    • Otherwise, return the absolute value of the remaining stone.

There are mainly Four approach to solve this problem – 

  1. Sorting 
  2. Binary Search 
  3. Heap
  4. Bucket Sort 

1. Sorting

  • Time complexity: O(n^2logn)
  • Space complexity: O(1) or O(n)depending on the sorting algorithm.

2. Binary Search

Time & Space Complexity
  • Time complexity: O(n^2)
  • Space complexity:  or O(n) depending on the sorting algorithm.

Where m is the number of calls made to add().

2. Binary Search

Time & Space Complexity
  • Time complexity: O(n^2)
  • Space complexity: or O(n) depending on the sorting algorithm.

3. Heap

Time & Space Complexity
  • Time complexity: O(nlog⁡n)
  • Space complexity: or O(n) depending on the sorting algorithm.

4. Bucket Sort

Time & Space Complexity
  • Time complexity:  O(n+w)
  • Space complexity: O(w)

More Articles