Skip to content

Commit 207ac80

Browse files
author
Parth Shah
authored
Update 973. K Closest Points to Origin.py
1 parent 77adb47 commit 207ac80

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
"""
2+
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
3+
4+
(Here, the distance between two points on a plane is the Euclidean distance.)
5+
6+
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
7+
8+
9+
10+
Example 1:
11+
12+
Input: points = [[1,3],[-2,2]], K = 1
13+
Output: [[-2,2]]
14+
Explanation:
15+
The distance between (1, 3) and the origin is sqrt(10).
16+
The distance between (-2, 2) and the origin is sqrt(8).
17+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
18+
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
19+
Example 2:
20+
21+
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
22+
Output: [[3,3],[-2,4]]
23+
(The answer [[-2,4],[3,3]] would also be accepted.)
24+
25+
26+
Note:
27+
28+
1 <= K <= points.length <= 10000
29+
-10000 < points[i][0] < 10000
30+
-10000 < points[i][1] < 10000
31+
"""
132
class Solution:
233
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
334
def dist(x,y):
@@ -6,4 +37,4 @@ def dist(x,y):
637
for x,y in points:
738
hmap[x,y] = dist(x,y)
839
hmap_sorted = sorted(hmap.items(),key = lambda a:a[1])
9-
return [key for key,value in hmap_sorted[:K]]
40+
return [key for key,value in hmap_sorted[:K]]

0 commit comments

Comments
 (0)