Created
February 9, 2020 22:17
-
-
Save HauptJ/7842456b765c839d8d20be107bd4f1a6 to your computer and use it in GitHub Desktop.
973. K Closest Points to Origin - https://leetcode.com/problems/k-closest-points-to-origin/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]: | |
""" | |
in: List, K | |
out List | |
Time: NLogN due to sorting | |
Space: N due to dictionary | |
""" | |
if not points: | |
return [] | |
resd = {} | |
res = [] | |
for point in points: | |
resd[tuple(point)] = point[0]*point[0] + point[1]*point[1] | |
resd = sorted(resd.items(), key=lambda x: x[1]) | |
for i in range(0, K, 1): | |
res.append(list(resd[i][0])) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment