Skip to content

Commit 1c39ed3

Browse files
authored
Create 973-K-Closest-Points-to-Origin.py
1 parent 26b82d9 commit 1c39ed3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

973-K-Closest-Points-to-Origin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
3+
pts = []
4+
for x, y in points:
5+
dist = (abs(x - 0) ** 2) + (abs(y - 0) ** 2)
6+
pts.append([dist, x, y])
7+
8+
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

0 commit comments

Comments
 (0)