Solution 1

  • TimeO(n log n)
  • SpaceO(n)

where, n is the length of points
Solved in 8 mins, all by yourself! Good job!

Python
from heapq import heapify, heappush, nsmallest
class Solution:
    '''
    Time Complexity: O(n log n)
    Space Complexity: O(n)
    where, n is the length of points
    Solved in 8 mins, all by yourself! Good job!
    '''
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        heap = []
        for i, [x, y] in enumerate(points):
            heappush(heap, ((x**2+y**2)**(0.5), i, x, y))
        return [(x,y) for d, i, x, y in nsmallest(k, heap)]
Leet Code/python.py · L2114–2127