Skip to content

Commit 510f60f

Browse files
Chaitra-Bhat383ignacio-chiazzo
authored andcommitted
k closest points to origin
1 parent a3d646a commit 510f60f

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).
3+
4+
The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).
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+
Example 1
9+
10+
Input: points = [[1,3],[-2,2]], k = 1
11+
Output: [[-2,2]]
12+
Explanation:
13+
The distance between (1, 3) and the origin is sqrt(10).
14+
The distance between (-2, 2) and the origin is sqrt(8).
15+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
16+
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
17+
18+
Example 2:
19+
20+
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
21+
Output: [[3,3],[-2,4]]
22+
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
23+
*/
24+
25+
/**
26+
* @param {number[][]} points
27+
* @param {number} k
28+
* @return {number[][]}
29+
*/
30+
31+
var kClosest = function(points, k) {
32+
const queue = [];
33+
34+
for (let i = 0; i < points.length; i++) {
35+
queue.push(points[i]);
36+
queue.sort((a, b) => ((a[0] * a[0]) + (a[1] * a[1])) - ((b[0] * b[0]) + (b[1] * b[1])));
37+
38+
if (queue.length > k) {
39+
queue.pop();
40+
}
41+
}
42+
43+
return queue;
44+
};
45+
46+
module.exports.kClosest = kClosest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require("assert");
2+
var kClosest = require("../../LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin").kClosest;
3+
4+
function test1() {
5+
var points = [[1,3],[-2,2]];
6+
var output = [[-2,2]];
7+
assert.strictEqual(kClosest(points,1), output);
8+
}
9+
10+
function test2() {
11+
var points = [[3,3],[5,-1],[-2,4]];
12+
var output = [[-2,4],[3,3]];
13+
assert.strictEqual(kClosest(points,2), output);
14+
}
15+
16+
function test() {
17+
test1();
18+
test2();
19+
}
20+
21+
module.exports.test = test;
22+

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
7070
| [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ |
7171
| [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ |
7272
| [Gas Station](/LeetcodeProblems/Algorithms/GasStation/index.js) | Medium | https://leetcode.com/problems/gas-station/description/|
73+
| [K Closest Points to Origin](/LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js/) | Medium | https://leetcode.com/problems/k-closest-points-to-origin/
7374
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
7475
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
7576
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
 (0)