개발공부/algorithm
[Leetcode][python] 973: K closest point to the origin
so.py
2021. 4. 28. 13:43
Problem
- Leetcode - 973: Closest point to the origin
- Level: Medium
- Link
K Closest Points to Origin - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
My Approach
import math
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
coords = []
for p in range(len(points)):
x, y = points[p][0], points[p][1]
dist = math.sqrt(x**2 + y**2)
coords.append([x, y, dist])
sort_orders = sorted(coords, key=lambda x: x[2], reverse=False)
res = [sort_orders[i][0:2] for i in range(len(sort_orders))]
return (res[0:k])