|
| 1 | +# K Closest Points to Origin |
| 2 | + |
| 3 | +Given a list of points in the form [[x1, y1], [x2, y2], ... [xn, yn]] and an integer k, find the k closest points to the |
| 4 | +origin (0, 0) on the 2D plane. |
| 5 | + |
| 6 | +The distance between two points (x, y) and (a, b) is calculated using the formula: |
| 7 | + |
| 8 | +√(x1 - a2)2 + (y1 - b2)2 |
| 9 | + |
| 10 | +Return the k closest points in any order. |
| 11 | + |
| 12 | +## Examples |
| 13 | + |
| 14 | +```text |
| 15 | +Input: |
| 16 | +
|
| 17 | +points = [[3,4],[2,2],[1,1],[0,0],[5,5]] |
| 18 | +k = 3 |
| 19 | +
|
| 20 | +Output: |
| 21 | +[[2,2],[1,1],[0,0]] |
| 22 | +
|
| 23 | +Also Valid: |
| 24 | +
|
| 25 | +[[2,2],[0,0],[1,1]] |
| 26 | +[[1,1],[0,0],[2,2]] |
| 27 | +[[1,1],[2,2],[0,0]] |
| 28 | +... |
| 29 | +[[0,0],[1,1],[2,2]] |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +```text |
| 35 | +Input: points = [[3,3],[5,-1],[-2,4]], k = 2 |
| 36 | +Output: [[3,3],[-2,4]] |
| 37 | +Explanation: The answer [[-2,4],[3,3]] would also be accepted. |
| 38 | +``` |
| 39 | + |
| 40 | +```text |
| 41 | +Input: points = [[1,3],[-2,2]], k = 1 |
| 42 | +Output: [[-2,2]] |
| 43 | +
|
| 44 | +Explanation: |
| 45 | +The distance between (1, 3) and the origin is sqrt(10). |
| 46 | +The distance between (-2, 2) and the origin is sqrt(8). |
| 47 | +Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. |
| 48 | +We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. |
| 49 | +``` |
| 50 | + |
| 51 | +## Solution |
| 52 | + |
| 53 | +- [Approach 1](#approach-1-sorting) |
| 54 | +- [Approach 2](#approach-2-max-heap) |
| 55 | + |
| 56 | +### Approach 1: Sorting |
| 57 | + |
| 58 | +The simplest approach is to sort calculate the distance of each point from the origin and sort the points based on their |
| 59 | +distance. This approach has a time complexity of O(n log n) where n is the number of points in the array, and a space |
| 60 | +complexity of O(n) (to store the sorted array of distances). |
| 61 | + |
| 62 | +### Approach 2: Max Heap |
| 63 | + |
| 64 | +This problem can be solved using a similar approach to the one used to solve [Kth Largest Element in an Array](../topklargest/README.md). The key |
| 65 | +difference is that we need to store the k closest points to the origin, rather than the k largest elements. Since we are |
| 66 | +looking for the k smallest elements, we need a max-heap, rather than a min-heap. |
| 67 | + |
| 68 | +By default, python's heapq module implements a min-heap, but we can make it behave like a max-heap by negating the values |
| 69 | +of everything we push onto it. |
| 70 | + |
| 71 | +We add the first k points to the heap by pushing a tuple containing the negative of the distance from the origin, and the |
| 72 | +index of the point. After that is finished, our heap contains the k closest points to the origin that we've seen so far, |
| 73 | +with the point furthest from the origin at the root of the heap. |
| 74 | + |
| 75 | +For each point after the first k, we calculate the distance from the origin and compare it with the root of the heap. If |
| 76 | +the current point is closer to the origin than the root of the heap, we pop the root and push the current point into the |
| 77 | +heap. This way, the heap will always contain the k closest points to the origin we've seen so far. |
| 78 | + |
| 79 | +At the end of the iteration, the heap will contain the k closest points to the origin. We can iterate over each point in |
| 80 | +the heap and return the point associated with each tuple. |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +#### Complexity Analysis |
| 96 | + |
| 97 | +##### Time Complexity: O(n log k) |
| 98 | + |
| 99 | +Where n is the number of points in the array and k is the input parameter. We iterate over |
| 100 | +all points, and in the worst case, we both push and pop each point from the heap, which takes O(log k) time per point. |
| 101 | + |
| 102 | +##### Space Complexity: O(k) |
| 103 | + |
| 104 | +Where k is the input parameter. The space is used by the heap to store the k closest points to the origin. |
0 commit comments