Skip to content

Commit fecfdf4

Browse files
committed
update readme
1 parent 0c5ad55 commit fecfdf4

File tree

5 files changed

+95
-187
lines changed

5 files changed

+95
-187
lines changed

1014-k-closest-points-to-origin/1014-k-closest-points-to-origin.py

Lines changed: 0 additions & 110 deletions
This file was deleted.

1014-k-closest-points-to-origin/README.md

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
# time complexity: O(nlogk)
2-
# space complexity: O(k)
3-
from heapq import heapify, heappop
1+
from heapq import heapify, heappop, heappush
42
from math import sqrt
53
from typing import List
64

5+
# time complexity: O(nlogk)
6+
# space complexity: O(k)
7+
class Solution:
8+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
9+
maxHp = [(-(x ** 2 + y ** 2), x, y) for x, y in points[:k]]
10+
heapify(maxHp)
11+
for currX, currY in points[k:]:
12+
dist = -(currX ** 2 + currY ** 2)
13+
if dist > maxHp[0][0]:
14+
heappop(maxHp)
15+
heappush(maxHp, (dist, currX, currY))
16+
17+
return [[x, y] for _, x, y in maxHp]
718

19+
# time complexity: O(nlogn)
20+
# space complexity: O(k)
821
class Solution:
922
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
1023
minHeap = [(sqrt(x**2 + y**2), x, y) for x, y in points]
@@ -15,12 +28,83 @@ def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
1528
result.append([currX, currY])
1629
k -= 1
1730
return result
31+
32+
# time complexity: O(n)
33+
# space complexity: O(n)
34+
class Solution:
35+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
36+
distances = [self.distance(point) for point in points]
37+
remaining = [i for i in range(len(points))]
38+
left, right = 0, max(distances)
39+
40+
closest = []
41+
while k:
42+
mid = (left + right) // 2
43+
closer, farther = self.splitDis(remaining, distances, mid)
44+
if len(closer) > k:
45+
remaining = closer
46+
right = mid
47+
else:
48+
k -= len(closer)
49+
closest.extend(closer)
50+
remaining = farther
51+
left = mid
52+
return [points[i] for i in closest]
53+
54+
def splitDis(self, remaining: List[int], distances: List[float],
55+
mid: int) -> List[List[int]]:
56+
closer, farther = [], []
57+
for index in remaining:
58+
if distances[index] <= mid:
59+
closer.append(index)
60+
else:
61+
farther.append(index)
62+
return [closer, farther]
1863

64+
def distance(self, point: List[int]) -> float:
65+
return point[0] ** 2 + point[1] ** 2
66+
67+
# time complexity: O(n)
68+
# space complexity: O(1)
69+
class Solution:
70+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
71+
return self.quickSelect(points, k)
72+
73+
def quickSelect(self, points: List[List[int]], k: int) -> List[List[int]]:
74+
left, right = 0, len(points) - 1
75+
pivotIdx = len(points)
76+
while pivotIdx != k:
77+
pivotIdx = self.partition(points, left, right)
78+
if pivotIdx < k:
79+
left = pivotIdx
80+
else:
81+
right = pivotIdx - 1
82+
83+
return points[:k]
84+
85+
def partition(self, points: List[List[int]], left: int, right: int) -> int:
86+
pivot = self.choosePivot(points, left, right)
87+
pivotDis = self.distance(pivot)
88+
while left < right:
89+
if self.distance(points[left]) >= pivotDis:
90+
points[left], points[right] = points[right], points[left]
91+
right -= 1
92+
else:
93+
left += 1
94+
95+
if self.distance(points[left]) < pivotDis:
96+
left += 1
97+
return left
98+
99+
def choosePivot(self, points: List[List[int]], left: int, right: int) -> List[int]:
100+
return points[left + (right - left) // 2]
101+
102+
def distance(self, point: List[int]) -> int:
103+
return point[0] ** 2 + point[1] ** 2
19104

20105
points = [[1, 3], [-2, 2]]
21106
k = 1
22107
print(Solution().kClosest(points, k))
23-
24108
points = [[3, 3], [5, -1], [-2, 4]]
25109
k = 2
26110
print(Solution().kClosest(points, k))

README.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,3 @@ It helps others discover the repo and keeps the project growing.
115115
---
116116

117117
Feedback / Questions → open an Issue or reach out on [LinkedIn](https://www.linkedin.com/in/hogan-l/)
118-
119-
<!---LeetCode Topics Start-->
120-
# LeetCode Topics
121-
## Array
122-
| |
123-
| ------- |
124-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
125-
## Math
126-
| |
127-
| ------- |
128-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
129-
## Divide and Conquer
130-
| |
131-
| ------- |
132-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
133-
## Geometry
134-
| |
135-
| ------- |
136-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
137-
## Sorting
138-
| |
139-
| ------- |
140-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
141-
## Heap (Priority Queue)
142-
| |
143-
| ------- |
144-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
145-
## Quickselect
146-
| |
147-
| ------- |
148-
| [1014-k-closest-points-to-origin](https://github.com/hogan-tech/leetcode-solution/tree/master/1014-k-closest-points-to-origin) |
149-
<!---LeetCode Topics End-->
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<h2> 8586 316
2-
973. K Closest Points to Origin</h2><hr><div><p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane and an integer <code>k</code>, return the <code>k</code> closest points to the origin <code>(0, 0)</code>.</p>
1+
<h2><a href="https://leetcode.com/problems/k-closest-points-to-origin">1014. K Closest Points to Origin</a></h2><h3>Medium</h3><hr><p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane and an integer <code>k</code>, return the <code>k</code> closest points to the origin <code>(0, 0)</code>.</p>
32

4-
<p>The distance between two points on the <strong>X-Y</strong> plane is the Euclidean distance (i.e., <code>(x<sub>1</sub> - x<sub>2</sub>)<sup>2</sup> + (y<sub>1</sub> - y<sub>2</sub>)<sup>2</sup></code>).</p>
3+
<p>The distance between two points on the <strong>X-Y</strong> plane is the Euclidean distance (i.e., <code>&radic;(x<sub>1</sub> - x<sub>2</sub>)<sup>2</sup> + (y<sub>1</sub> - y<sub>2</sub>)<sup>2</sup></code>).</p>
54

65
<p>You may return the answer in <strong>any order</strong>. The answer is <strong>guaranteed</strong> to be <strong>unique</strong> (except for the order that it is in).</p>
76

87
<p>&nbsp;</p>
98
<p><strong class="example">Example 1:</strong></p>
10-
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg" style="width: 400px; height: 400px;">
11-
<pre><strong>Input:</strong> points = [[1,3],[-2,2]], k = 1
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg" style="width: 400px; height: 400px;" />
10+
<pre>
11+
<strong>Input:</strong> points = [[1,3],[-2,2]], k = 1
1212
<strong>Output:</strong> [[-2,2]]
1313
<strong>Explanation:</strong>
1414
The distance between (1, 3) and the origin is sqrt(10).
@@ -19,7 +19,8 @@ We only want the closest k = 1 points from the origin, so the answer is just [[-
1919

2020
<p><strong class="example">Example 2:</strong></p>
2121

22-
<pre><strong>Input:</strong> points = [[3,3],[5,-1],[-2,4]], k = 2
22+
<pre>
23+
<strong>Input:</strong> points = [[3,3],[5,-1],[-2,4]], k = 2
2324
<strong>Output:</strong> [[3,3],[-2,4]]
2425
<strong>Explanation:</strong> The answer [[-2,4],[3,3]] would also be accepted.
2526
</pre>
@@ -31,4 +32,3 @@ We only want the closest k = 1 points from the origin, so the answer is just [[-
3132
<li><code>1 &lt;= k &lt;= points.length &lt;= 10<sup>4</sup></code></li>
3233
<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
3334
</ul>
34-
</div>

0 commit comments

Comments
 (0)