Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
- dist = [[math.sqrt(x**2 + y**2), x, y] for (x, y) in points]
- heapq.heapify(dist)
- res = []
- for i in range(k):
- d, x, y = heapq.heappop(dist)
- res.append([x, y])
- return res
- """
- 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)
- 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"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement