Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 533 Bytes

973.md

File metadata and controls

18 lines (16 loc) · 533 Bytes

973. K Closest Points to Origin

Solution 1 (time O(n*log(k)), space O(k))

class Solution(object):
    def kClosest(self, points, k):
        """
        :type points: List[List[int]]
        :type k: int
        :rtype: List[List[int]]
        """
        dists = [(-p[0] * p[0] - p[1] * p[1], p) for p in points[:k]]
        heapq.heapify(dists)
        for p in points[k:]:
            heapq.heappushpop(dists, (-p[0] * p[0] - p[1] * p[1], p))
        return list(map(lambda x: x[1], dists))