Skip to content

Commit 26879bf

Browse files
Merge pull request #2977 from coopers/0973
Update 0973-k-closest-points-to-origin.py
2 parents 520ad81 + 976ac80 commit 26879bf

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
3-
pts = []
3+
minHeap = []
44
for x, y in points:
5-
dist = (abs(x - 0) ** 2) + (abs(y - 0) ** 2)
6-
pts.append([dist, x, y])
7-
5+
dist = (x ** 2) + (y ** 2)
6+
minHeap.append((dist, x, y))
7+
8+
heapq.heapify(minHeap)
89
res = []
9-
heapq.heapify(pts)
10-
for i in range(k):
11-
dist, x, y = heapq.heappop(pts)
12-
res.append([x, y])
13-
return res
10+
for _ in range(k):
11+
_, x, y = heapq.heappop(minHeap)
12+
res.append((x, y))
13+
return res

0 commit comments

Comments
 (0)