Advertisement
smj007

Untitled

Feb 18th, 2024
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. class Solution:
  2.     def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
  3.  
  4.         dist = [[math.sqrt(x**2 + y**2), x, y] for (x, y) in points]
  5.         heapq.heapify(dist)
  6.         res = []
  7.  
  8.         for i in range(k):
  9.             d, x, y = heapq.heappop(dist)
  10.             res.append([x, y])
  11.  
  12.         return res
  13.        
  14. """
  15. Time Complexity : O(N + klogN), constructing the heap from given array of points P can be done in O(N) time. Then each heap pop operation would take O(logN) time which will be called for k times. Thus overall time will be O(N + klogN)
  16. Space Complexity : O(1), we are doing it in-place. If input modification is not allowed, use a copy of P and that would take O(N) space"""
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement