From 5a9c2e3a59b2d2fdaca85e35ff49eb44cd1862e7 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 20 Jan 2019 15:09:17 -0500 Subject: [PATCH 1/2] Added Swap Nodes in Pairs --- DesignDataStructure/designI.js | 36 +++++++++++++++ LeetcodeProblems/Permutations.js | 2 +- LeetcodeProblems/Swap_Nodes_In_Pairs.js | 58 +++++++++++++++++++++++++ README.md | 1 + 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 DesignDataStructure/designI.js create mode 100644 LeetcodeProblems/Swap_Nodes_In_Pairs.js diff --git a/DesignDataStructure/designI.js b/DesignDataStructure/designI.js new file mode 100644 index 0000000..e3112c5 --- /dev/null +++ b/DesignDataStructure/designI.js @@ -0,0 +1,36 @@ +/* +Design an efficient data structure for given operations +Design a Data Structure for the following operations. +The data structure should be efficient enough to accommodate the operations according to their frequency. +1) findMin() : Returns the minimum item. + Frequency: Most frequent + +2) findMax() : Returns the maximum item. + Frequency: Most frequent + +3) deleteMin() : Delete the minimum item. + Frequency: Moderate frequent + +4) deleteMax() : Delete the maximum item. + Frequency: Moderate frequent + +5) Insert() : Inserts an item. + Frequency: Least frequent + +6) Delete() : Deletes an item. + Frequency: Least frequent. +*/ + + +class effStructure { + this.maxHeap = []; + this.minHeap = []; +} + + +1) findMin(): O(1) +2) findMax(): O(1) +3) deleteMin(): O(log N) +4) deleteMax(): O(log N) +5) Insert(log N) +6) Delete: O(log N) \ No newline at end of file diff --git a/LeetcodeProblems/Permutations.js b/LeetcodeProblems/Permutations.js index ad02c63..de86a56 100644 --- a/LeetcodeProblems/Permutations.js +++ b/LeetcodeProblems/Permutations.js @@ -45,4 +45,4 @@ var main = function() { console.log(permute([1,2,3,4,5,6])); } -exports.module.main = main; \ No newline at end of file +module.exports.main = main; diff --git a/LeetcodeProblems/Swap_Nodes_In_Pairs.js b/LeetcodeProblems/Swap_Nodes_In_Pairs.js new file mode 100644 index 0000000..7ba45f1 --- /dev/null +++ b/LeetcodeProblems/Swap_Nodes_In_Pairs.js @@ -0,0 +1,58 @@ +/* +Swap Nodes in Pairs +https://leetcode.com/problems/swap-nodes-in-pairs/ + +Given a linked list, swap every two adjacent nodes and return its head. + +Example: + +Given 1->2->3->4, you should return the list as 2->1->4->3. +Note: + +Your algorithm should use only constant extra space. +You may not modify the values in the list's nodes, only nodes itself may be changed. +*/ + +var ListNode = require('../UtilsClasses/ListNode').ListNode; + +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var swapPairs = function(head) { + if(head === null || head.next === null) + return head + var previous = null; + var current = head; + var following = (head.next != null) ? head.next.next : null; + head = head.next; + + while(current !== null && current.next !== null) { + var next = current.next; + next.next = current; + if(previous != null) + previous.next = next; + current.next = following; + previous = current; + current = following; + following = (current !== null && current.next != null) ? current.next.next : null; + } + + return head; +}; + +var main = function() { + console.log(swapPairs(ListNode.linkenList([1,2,3,4]))); + console.log(swapPairs(ListNode.linkenList([]))); + console.log(swapPairs(ListNode.linkenList([1]))); + console.log(swapPairs(ListNode.linkenList([1,2]))); +} + +module.exports.main = main; diff --git a/README.md b/README.md index e6790f8..740cf2c 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Solutions of algorithm problems using Javascript | [merge k sorted lists ](/LeetcodeProblems/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ | | [Subarray Sum Equals K ](/LeetcodeProblems/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ | | [3Sum ](/LeetcodeProblems/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ | +| [Swap Nodes in Pairs](/LeetcodeProblems/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ | | [Add Two Numbers ](/LeetcodeProblems/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ | | [Clone Graph ](/LeetcodeProblems/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ | | [Coin Change ](/LeetcodeProblems/Coin_Change.js) | Medium | https://leetcode.com/problems/coin-change/ | From 94b554c51c3db4c1b7461ffc563295c8ff4e146d Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 20 Jan 2019 19:42:18 -0500 Subject: [PATCH 2/2] Added number of Islands --- LeetcodeProblems/Number_of_Islands.js | 75 +++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 76 insertions(+) create mode 100644 LeetcodeProblems/Number_of_Islands.js diff --git a/LeetcodeProblems/Number_of_Islands.js b/LeetcodeProblems/Number_of_Islands.js new file mode 100644 index 0000000..bdbc6ac --- /dev/null +++ b/LeetcodeProblems/Number_of_Islands.js @@ -0,0 +1,75 @@ +/* +Number of Islands +https://leetcode.com/problems/number-of-islands/ + +Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. + +Example 1: + +Input: +11110 +11010 +11000 +00000 + +Output: 1 +Example 2: + +Input: +11000 +11000 +00100 +00011 + +Output: 3 +*/ + +/* + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function(grid) { + if(grid.length === 0) + return 0; + + var countIslands = 0; + const rowsCount = grid.length; + const columnsCount = grid[0].length; + for(var i = 0; i < rowsCount; i++) { + for(var j = 0; j < columnsCount; j++) { + if(grid[i][j] == 1) { + countIslands++; + colorIsland(grid, i, j, rowsCount, columnsCount); + } + } + } + + return countIslands; +}; + +var colorIsland = function(grid, i, j, rowsCount, columnsCount) { + if(i < 0 || j < 0 || i >= rowsCount || j >= columnsCount || grid[i][j] == 0) + return; + + grid[i][j] = 0; + + colorIsland(grid, i - 1, j, rowsCount, columnsCount); + colorIsland(grid, i + 1, j, rowsCount, columnsCount); + colorIsland(grid, i, j - 1, rowsCount, columnsCount); + colorIsland(grid, i, j + 1, rowsCount, columnsCount); +} + +var main = function() { + console.log(numIslands([[1]])); + console.log(numIslands([])); + console.log(numIslands( + [ + ["1","1","1","1","0"], + ["1","1","0","1","0"], + ["1","1","0","0","0"], + ["0","0","0","0","0"] + ]) + ); +} + +module.exports.main = main; \ No newline at end of file diff --git a/README.md b/README.md index 740cf2c..f314b96 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Solutions of algorithm problems using Javascript | [merge k sorted lists ](/LeetcodeProblems/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ | | [Subarray Sum Equals K ](/LeetcodeProblems/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ | | [3Sum ](/LeetcodeProblems/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ | +| [NumberOfIslands ](/LeetcodeProblems/Number_of_Islands.js) | Medium | https://leetcode.com/problems/number-of-islands/ | | [Swap Nodes in Pairs](/LeetcodeProblems/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ | | [Add Two Numbers ](/LeetcodeProblems/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ | | [Clone Graph ](/LeetcodeProblems/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ |