Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created February 9, 2020 22:17
Show Gist options
  • Save HauptJ/7842456b765c839d8d20be107bd4f1a6 to your computer and use it in GitHub Desktop.
Save HauptJ/7842456b765c839d8d20be107bd4f1a6 to your computer and use it in GitHub Desktop.
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