class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
heap = []
heapq.heapify(heap)
def getDest(x, y):
return (x**2 + y**2)**0.5
for x, y in points:
dest = getDest(x, y)
if len(heap) == k:
heapq.heappush(heap, (-dest, [x, y]))
heapq.heappop(heap)
else:
heapq.heappush(heap, (-dest, [x, y]))
return [value for key, value in heap]
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
heap = []
heapq.heapify(heap)
def getDest(x, y):
return (x**2 + y**2)**0.5
for x, y in points:
dest = getDest(x, y)
if len(heap) == k:
heapq.heappushpop(heap, (-dest, [x, y]))
else:
heapq.heappush(heap, (-dest, [x, y]))
return [value for key, value in heap]