LeetCode/0973_ K_Closest_Points_to_Origin.py
def closest_points(self, points: List[Tuple[float, float]], k: int) -> List[Tuple[float, float]]:
        """
        Finds the K points closest to the origin.

        The implementation pushes the points on a Heap with their key being the distance to the origin, then removes K elements from the heap.
        I chose to go with a more verbose implementation to show how it can be done, but alternatively one could do:

        >>> from heapq import nsmallest
        ... nsmallest(k, points, key=self.distance)
        """
        
        heap = []
        for point in points:
            heappush(heap, (self.distance(point), point))

        return [heappop(heap)[1] for _ in range(k)]

        Finds the K points closest to the origin.

        The implementation pushes the points on a Heap with their key being the distance to the origin, then removes K elements from the heap.
        I chose to go with a more verbose implementation to show how it can be done, but alternatively one could do:

        >>> from heapq import nsmallest
        ... nsmallest(k, points, key=self.distance)
        
Finds the K points closest to the origin.
The implementation pushes the points on a Heap with their key being the distance to the origin, then removes K elements from the heap.
I chose to go with a more verbose implementation to show how it can be done, but alternatively one could do.


Finds the K points closest to the origin . The implementation pushes the points on a Heap with their key being the distance to the origin then removes K elements from the heap . I chose to go with a more verbose implementation to show how it can be done but alternatively one could do .
def closest_points(self, points: List[Tuple[float, float]], k: int) -> List[Tuple[float, float]]:
        heap = []
        for point in points:
            heappush(heap, (self.distance(point), point))
        return [heappop(heap)[1] for _ in range(k)]
def closest_points ( self , points : List [ Tuple [ float , float ] ] , k : int ) -> List [ Tuple [ float , float ] ] : heap = [ ] for point in points : heappush ( heap , ( self . distance ( point ) , point ) ) return [ heappop ( heap ) [ 1 ] for _ in range ( k ) ]
Finds the K points closest to the origin.
Finds the K points closest to the origin .
"""
        Finds the K points closest to the origin.

        The implementation pushes the points on a Heap with their key being the distance to the origin, then removes K elements from the heap.
        I chose to go with a more verbose implementation to show how it can be done, but alternatively one could do:

        >>> from heapq import nsmallest
        ... nsmallest(k, points, key=self.distance)
        """
self
points
List[Tuple[float, float]]
k
int