File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,056 LeetCode solutions in JavaScript
1
+ # 1,057 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
781
781
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
782
782
971|[ Flip Binary Tree To Match Preorder Traversal] ( ./solutions/0971-flip-binary-tree-to-match-preorder-traversal.js ) |Medium|
783
783
972|[ Equal Rational Numbers] ( ./solutions/0972-equal-rational-numbers.js ) |Hard|
784
+ 973|[ K Closest Points to Origin] ( ./solutions/0973-k-closest-points-to-origin.js ) |Medium|
784
785
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
785
786
977|[ Squares of a Sorted Array] ( ./solutions/0977-squares-of-a-sorted-array.js ) |Easy|
786
787
985|[ Sum of Even Numbers After Queries] ( ./solutions/0985-sum-of-even-numbers-after-queries.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 973. K Closest Points to Origin
3
+ * https://leetcode.com/problems/k-closest-points-to-origin/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of points where points[i] = [xi, yi] represents a point on the X-Y
7
+ * plane and an integer k, return the k closest points to the origin (0, 0).
8
+ *
9
+ * The distance between two points on the X-Y plane is the Euclidean distance
10
+ * (i.e., √(x1 - x2)2 + (y1 - y2)2).
11
+ *
12
+ * You may return the answer in any order. The answer is guaranteed to be unique
13
+ * (except for the order that it is in).
14
+ */
15
+
16
+ /**
17
+ * @param {number[][] } points
18
+ * @param {number } k
19
+ * @return {number[][] }
20
+ */
21
+ var kClosest = function ( points , k ) {
22
+ const getDistance = ( x , y ) => x * x + y * y ;
23
+
24
+ return points
25
+ . map ( point => ( { point, dist : getDistance ( point [ 0 ] , point [ 1 ] ) } ) )
26
+ . sort ( ( a , b ) => a . dist - b . dist )
27
+ . slice ( 0 , k )
28
+ . map ( item => item . point ) ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments