diff --git a/LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js b/LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js new file mode 100644 index 0000000..fa2fca5 --- /dev/null +++ b/LeetcodeProblems/Algorithms/K_Closest_Points_to_Origin.js @@ -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; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/K_Closest_Points_to_Origin_Test.js b/LeetcodeProblemsTests/Algorithms/K_Closest_Points_to_Origin_Test.js new file mode 100644 index 0000000..3164742 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/K_Closest_Points_to_Origin_Test.js @@ -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; + diff --git a/README.md b/README.md index 2014395..0b2e13d 100644 --- a/README.md +++ b/README.md @@ -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/ |