-
Notifications
You must be signed in to change notification settings - Fork 101
K Closest Points to Origin #77
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1410a14
Added eslint
Chaitra-Bhat383 426fb54
EsLint and Tests fix
Chaitra-Bhat383 d49b053
Update README.md
Chaitra-Bhat383 a01edee
Update README.md
Chaitra-Bhat383 0d38e47
Update README.md
Chaitra-Bhat383 ed79cdf
updated readme
Chaitra-Bhat383 f3986e1
updated readme
Chaitra-Bhat383 b518c45
Revert "Revert "readme.md created""
Chaitra-Bhat383 c76622d
updated readme
Chaitra-Bhat383 26a70e1
Merge pull request #1 from Chaitra-Bhat383/revert-78-revert-75-patch-3
Chaitra-Bhat383 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
22 changes: 22 additions & 0 deletions
LeetcodeProblemsTests/Algorithms/K_Closest_Points_to_Origin_Test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.