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
+ """
1
32
class Solution :
2
33
def kClosest (self , points : List [List [int ]], K : int ) -> List [List [int ]]:
3
34
def dist (x ,y ):
@@ -6,4 +37,4 @@ def dist(x,y):
6
37
for x ,y in points :
7
38
hmap [x ,y ] = dist (x ,y )
8
39
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