Max-heap of size k based on distance. Use negative distance for max-heap in languages with min-heap only.
function kClosest(points, k): heap = [] for x, y in points: dist = xx + yy if len(heap) < k: heappush(heap, (-dist, x, y)) elif -dist > heap[0][0]: heapreplace(heap, (-dist, x, y)) return [[x, y] for _, x, y in heap]
time, space.