Problem
- Leetcode - 973: Closest point to the origin
- Level: Medium
- Link
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])
'개발공부 > algorithm' 카테고리의 다른 글
[Leetcode][python] 482: License key formatting - String (0) | 2021.05.01 |
---|---|
[Leetcode][python] 120: triangle - DP (0) | 2021.04.29 |
[Leetcode][python] 929: Unique email addresses (0) | 2021.04.28 |
[Leetcode][python] 1161: Maximum Level of sum of a binary tree - BFS (0) | 2021.04.27 |
[Leetcode][python] 257: binary tree paths - DFS (0) | 2021.04.27 |