파이썬 알고리즘 인터뷰 64번(리트코드 973) K Closest Points to Origin
https://leetcode.com/problems/k-closest-points-to-origin/
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
points.sort(key = lambda p: p[0]**2 + p[1]**2)
return points[:k]
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
heap = []
for x, y in points:
heapq.heappush(heap, (x**2 + y**2, x, y))
result = []
for _ in range(k):
dist, x, y = heapq.heappop(heap)
result.append([x, y])
return result