Skip to content

Commit b251b7e

Browse files
committed
Add solution #973
1 parent 00549b7 commit b251b7e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,056 LeetCode solutions in JavaScript
1+
# 1,057 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -781,6 +781,7 @@
781781
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
782782
971|[Flip Binary Tree To Match Preorder Traversal](./solutions/0971-flip-binary-tree-to-match-preorder-traversal.js)|Medium|
783783
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|
784785
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
785786
977|[Squares of a Sorted Array](./solutions/0977-squares-of-a-sorted-array.js)|Easy|
786787
985|[Sum of Even Numbers After Queries](./solutions/0985-sum-of-even-numbers-after-queries.js)|Easy|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)