973. K Closest Points to Origin

heap

left(0, 1) to right(2, 3) .=> √(x1 - x2)^2 + (y1 - y2)^2)

maintain max heap, over size k, pop max value

at last, remain top K smallest elements

time: O(nlogk), add to heap is O(logk), do n times

space: O(k)

quickselect with radompivot

time: O(n), worst O(n^2)

space: O(1)

quick select (while version)

Last updated

Was this helpful?