#973
K Closest Points to Origin
medium· Heap / Priority Queueruns: 0Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, and an integer k, return the k closest points to the origin (0, 0). The distance between two points is the Euclidean distance.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 282
import heapq
class Solution:
def kClosest(
self, points: list[list[int]], k: int
) -> list[list[int]]:
heap = []
for x, y in points:
heapq.heappush(heap, (x * x + y * y, [x, y]))
return [heapq.heappop(heap)[1] for _ in range(k)]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.