Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Chaitra-Bhat383 wants to merge 10 commits into ignacio-chiazzo:master from Chaitra-Bhat383:master
Closed
Show file tree
Hide file tree
Changes from 5 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
View file Open in desktop
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;
View file Open in desktop
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

135 changes: 1 addition & 134 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,134 +1 @@
# Algorithms-Javascript

Solutions of algorithm problems using Javascript. https://ignacio-chiazzo.github.io/Algorithms-Leetcode-Javascript/

### Structure
The solutions are located under `/LeetcodeProblems`. Each problem has a test file located under `/LeetcodeProblemsTest`.

### Run Tests

**Unit tests:** To run all the test run `node Test.js` in the console. To run a specific problem in your console run `node <problem_file_path>` (e.g. `node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js`).

**Linter:** This repository uses [`es-lint`](https://eslint.org/docs/latest/user-guide/command-line-interface). To run all the tests you would need to install the packages by running `npm install` followed by `npx eslint LeetcodeProblems LeetcodeProblemsTests` which will run the eslint in all problems and tests. You can also use the [flag `--fix`](https://eslint.org/docs/latest/user-guide/command-line-interface#fixing-problems) which will automatically fix some of the errors.

### Leetcode Problems

| Name | Level | Link |
| - | - | - |
| [Edit Distance ](/LeetcodeProblems/Algorithms/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ |
| [Remove Invalid Parentheses ](/LeetcodeProblems/Algorithms/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ |
| [Longest Consecutive Sequence ](/LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |
| [Minimum Window Substring ](/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js) | Hard | https://leetcode.com/problems/minimum-window-substring/ |
| [Regular Expression Matching ](/LeetcodeProblems/Algorithms/Regular_Expression_Matching.js) | Hard | https://leetcode.com/problems/regular-expression-matching/ |
| [NQueens ](/LeetcodeProblems/Algorithms/NQueens.js) | Hard | https://leetcode.com/problems/n-queens/ |
| [merge k sorted lists ](/LeetcodeProblems/Algorithms/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ |
| [Set Matrix Zeroes](/LeetcodeProblems/Algorithms/Set_Matrix_Zeroes.js) | Hard | https://leetcode.com/problems/set-matrix-zeroes/ |
| [Subarray Sum Equals K ](/LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ |
| [3Sum Closest](/LeetcodeProblems/Algorithms/3SumClosest.js) | Medium | https://leetcode.com/problems/3sum-closest/ |
| [3Sum ](/LeetcodeProblems/Algorithms/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ |
| [NumberOfIslands ](/LeetcodeProblems/Algorithms/Number_of_Islands.js) | Medium | https://leetcode.com/problems/number-of-islands/ |
| [Swap Nodes in Pairs](/LeetcodeProblems/Algorithms/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ |
| [Add Two Numbers ](/LeetcodeProblems/Algorithms/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ |
| [Clone Graph ](/LeetcodeProblems/Algorithms/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ |
| [Coin Change ](/LeetcodeProblems/Algorithms/Coin_Change.js) | Medium | https://leetcode.com/problems/coin-change/ |
| [Container With Most Water](/LeetcodeProblems/Algorithms/Container_With_Most_Water.js) | Medium | https://leetcode.com/problems/container-with-most-water/ |
| [Design Circular Deque ](/LeetcodeProblems/Algorithms/Design_Circular_Deque.js) | Medium | https://leetcode.com/problems/design-circular-deque/
| [Escape The Ghosts](/LeetcodeProblems/Algorithms/Escape_The_Ghosts.js) | Medium | https://leetcode.com/problems/escape-the-ghosts/ |
| [Find All Anagrams in a String](/LeetcodeProblems/Algorithms/Find_Anagrams.js) | Medium | https://leetcode.com/problems/find-all-anagrams-in-a-string/ |
| [Generate Parenthesis ](/LeetcodeProblems/Algorithms/Generate_Parenthesis.js) | Medium | https://leetcode.com/problems/generate-parentheses |
| [Group Anagrams ](/LeetcodeProblems/Algorithms/Group_Anagrams.js) | Medium | https://leetcode.com/problems/group-anagrams/
| [Kth Largest Element in an Array ](/LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array.js) | Medium | https://leetcode.com/problems/kth-largest-element-in-an-array/ |
| [Linked List Cycle II ](/LeetcodeProblems/Algorithms/Linked_List_Cycle_II.js) | Medium | https://leetcode.com/problems/linked-list-cycle-ii/ |
| [Longest Palindromic Substring ](/LeetcodeProblems/Algorithms/Longest_Palindromic_Substring.js) | Medium | https://leetcode.com/problems/longest-palindromic-substring/|
| [Longest Substring Without Reapeating Characters](/LeetcodeProblems/Algorithms/Longest_Substring.js) | Medium | https://leetcode.com/problems/longest-substring-without-repeating-characters|
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
| [Max Consecutive Ones III ](/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js) | Medium | https://leetcode.com/problems/max-consecutive-ones-iii |
| [Maximal Square ](/LeetcodeProblems/Algorithms/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ |
| [Minimum Add to Make Parentheses Valid ](/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js) | Medium | https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/ |
| [Minimum Size Subarray](/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js) | Medium | https://leetcode.com/problems/minimum-size-subarray-sum |
| [Permutations ](/LeetcodeProblems/Algorithms/Permutations.js) | Medium | https://leetcode.com/problems/permutations/ |
| [Permutations II ](/LeetcodeProblems/Algorithms/Permutations_II.js) | Medium | https://leetcode.com/problems/permutations-ii/ |
| [Permutation in String](/LeetcodeProblems/Algorithms/Permutations_In_String.js) | Medium | https://leetcode.com/problems/permutation-in-string/ |
| [Permutations Without Duplicates ](/LeetcodeProblems/Algorithms/Permutations_Without_Duplicates.js) | Medium | https://leetcode.com/problems/permutations/ |
| [Restore IP Addresses ](/LeetcodeProblems/Algorithms/Restore_IP_Addresses.js) | Medium | https://leetcode.com/problems/restore-ip-addresses/ |
| [SearchIng Rotated Sorted Array ](/LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array.js) | Medium | https://leetcode.com/problems/search-in-rotated-sorted-array/ |
| [Search a 2D Matrix ](/LeetcodeProblems/Algorithms/Search_a_2D_Matrix.js) | Medium | https://leetcode.com/problems/search-a-2d-matrix/ |
| [Search a 2D Matrix II ](/LeetcodeProblems/Algorithms/Search_a_2D_Matrix_II.js) | Medium | https://leetcode.com/problems/search-a-2d-matrix/ |
| [Simplify Path ](/LeetcodeProblems/Algorithms/Simplify_Path.js) | Medium | https://leetcode.com/problems/simplify-path/ |
| [Spiral Matrix ](/LeetcodeProblems/Algorithms/Spiral_Matrix.js) | Medium | https://leetcode.com/problems/spiral-matrix/ |
| [Subsets ](/LeetcodeProblems/Algorithms/Subsets.js) | Medium | https://leetcode.com/problems/subsets/ |
| [Unique Binary Search Trees ](/LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees.js) | Medium | https://leetcode.com/problems/unique-binary-search-trees/ |
| [Unique Paths ](/LeetcodeProblems/Algorithms/Unique_Paths.js) | Medium | https://leetcode.com/problems/unique-paths/ |
| [Verify Preorder Serialization of a Binary Tree ](/LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ |
| [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ |
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ |
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
| [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/|
| [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/ |
| [Maximun Subarray ](/LeetcodeProblems/Algorithms/Maximun_Subarray.js) | Easy | https://leetcode.com/problems/maximum-subarray |
| [Min Stack ](/LeetcodeProblems/Algorithms/Min_Stack.js) | Easy | https://leetcode.com/problems/min-stack/ |
| [Reverse String II ](/LeetcodeProblems/Algorithms/Reverse_String_II.js) | Easy | https://leetcode.com/problems/reverse-string-ii/ |
| [Same Tree ](/LeetcodeProblems/Algorithms/Same_Tree.js) | Easy | https://leetcode.com/problems/same-tree/ |
| [Sum Of Square Numbers ](/LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers.js) | Easy | https://leetcode.com/problems/sum-of-square-numbers/ |
| [Symmetric Tree ](/LeetcodeProblems/Algorithms/Symmetric_Tree.js) | Easy | https://leetcode.com/problems/symmetric-tree/ |
| [Valid Parentheses ](/LeetcodeProblems/Algorithms/Valid_Parentheses.js) | Easy | https://leetcode.com/problems/valid-parentheses/ |
| [Backspace String Compare ](/LeetcodeProblems/Algorithms/Backspace_String_Compare.js) | Easy | https://leetcode.com/problems/backspace-string-compare/ |
| [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
| [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
| [Majority Element](/LeetcodeProblems/Algorithms/Majority_Element.js) | Easy | https://leetcode.com/problems/majority-element/ |
| [Longest Common Prefix](/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js) | Easy | https://leetcode.com/problems/longest-common-prefix/ |
| [Two Sum](/LeetcodeProblems/Algorithms/2Sum.js) | Easy | https://leetcode.com/problems/two-sum/ |
| [Tic Tac Toe ](/LeetcodeProblems/Algorithms/Tic_Tac_Toe.js) | | |
| [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | |
| [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |
| [Award Budget Cuts](/LeetcodeProblems/Algorithms/Award_Budget_Cuts.js) | | |
| [Happy Number](https://leetcode.com/problems/happy-number/) | Easy | https://leetcode.com/problems/happy-number/ |
| [Shuffle String](https://leetcode.com/problems/shuffle-string/) | Easy | https://leetcode.com/problems/shuffle-string/ |

### Sorting Algorithms
| Algoritmhs |
| - |
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
| [Quick Sort](/SortingAlgorithms/QuickSort.js) |

### Databases
| Problems | Level | Link |
|-|-|-|
| [Trips and Users](/LeetcodeProblems/Databases/Trips_and_Users.sql) | Hard | https://leetcode.com/problems/trips-and-users/ |
| [Human Traffic of Stadium](/LeetcodeProblems/Databases/Human_Traffic_of_Stadium.sql) | Hard | https://leetcode.com/problems/human-traffic-of-stadium |
| [Rank Scores](/LeetcodeProblems/Databases/Rank_Scores.sql) | Medium | https://leetcode.com/problems/rank-scores |
| [Consecutive Numbers](/LeetcodeProblems/Databases/Consecutive_Numbers.sql) | Medium | https://leetcode.com/problems/consecutive-numbers |
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary |
| [Exchange Seats](/LeetcodeProblems/Databases/Exchange_Seats.sql) | Medium | https://leetcode.com/problems/exchange-seats |
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |
| [Combine Two Tables](/LeetcodeProblems/Databases/Combine_Two_Tables.sql) | Easy | https://leetcode.com/problems/combine-two-tables |
| [Second Highest Salary](/LeetcodeProblems/Databases/Second_highest_salary.sql)| Easy | https://leetcode.com/problems/second-highest-salary |
| [Customers Who Never Order](/LeetcodeProblems/Databases/Customers_Who_Never_Order.sql)| Easy | https://leetcode.com/problems/customers-who-never-order |
| [Reformat Department Table](/LeetcodeProblems/Databases/Reformat_Department_Table.sql) | Easy | https://leetcode.com/problems/reformat-department-table |
| [Employees Earning More Than Their Managers](/LeetcodeProblems/Databases/Employees_Earning_More_Than_Their_Managers.sql) | Easy | https://leetcode.com/problems/employees-earning-more-than-their-managers/ |
| [Delete Duplicate Emails](LeetcodeProblems/Databases/Delete_Duplicate_Emails.sql) | Easy | https://leetcode.com/problems/delete-duplicate-emails |
| [Rising Temperature](LeetcodeProblems/Databases/Rising_Temperature.sql) | Easy | https://leetcode.com/problems/rising-temperature |

### UtilsClasses

Other languages provides built-in classes (e.g Linked List, Tree, etc). This module contains util classes to use in your problems.

### Contributions

I'd be pleased to accept contributions. I'd be happy to discuss problems and solutions over a Pull Request or an Issue.

Each problem should have:

1) A description of the problem at the top of the file.
2) A test file with some test cases. The test file must export a `test()` function which should run all the tests of the file.
3) An entry of the problem in the list of solutions within the README file.

PR Example: https://github.com/ignacio-chiazzo/Algorithms-Leetcode-Javascript/pull/39
[Maximal Square ](/LeetcodeProblems/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/
Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the readme? It deleted all the previous staff.

Copy link
Contributor Author

@Chaitra-Bhat383 Chaitra-Bhat383 Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes did it now!

AltStyle によって変換されたページ (->オリジナル) /