Skip to content

k closest points to origin #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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).

The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)2 + (y1 - y2)2).

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).

Example 1

Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].

Example 2:

Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
*/

/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/

var kClosest = function(points, k) {
const queue = [];

for (let i = 0; i < points.length; i++) {
queue.push(points[i]);
queue.sort((a, b) => ((a[0] * a[0]) + (a[1] * a[1])) - ((b[0] * b[0]) + (b[1] * b[1])));

if (queue.length > k) {
queue.pop();
}
}

return queue;
};

module.exports.kClosest = kClosest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require("assert");
var kClosest = require("../../LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin").kClosest;

function test1() {
var points = [[1,3],[-2,2]];
var output = [[-2,2]];
assert.strictEqual(kClosest(points,1), output);
}

function test2() {
var points = [[3,3],[5,-1],[-2,4]];
var output = [[-2,4],[3,3]];
assert.strictEqual(kClosest(points,2), output);
}

function test() {
test1();
test2();
}

module.exports.test = test;

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
| [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/ |
| [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ |
| [Gas Station](/LeetcodeProblems/Algorithms/GasStation/index.js) | Medium | https://leetcode.com/problems/gas-station/description/|
| [K Closest Points to Origin](/LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js/) | Medium | https://leetcode.com/problems/k-closest-points-to-origin/
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
| [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/ |
Expand Down