Skip to content

Prob #28

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 2 commits into from
Jan 21, 2019
Merged

Prob #28

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
36 changes: 36 additions & 0 deletions DesignDataStructure/designI.js
Original file line number Diff line number Diff line change
@@ -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)
75 changes: 75 additions & 0 deletions LeetcodeProblems/Number_of_Islands.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion LeetcodeProblems/Permutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ var main = function() {
console.log(permute([1,2,3,4,5,6]));
}

exports.module.main = main;
module.exports.main = main;
58 changes: 58 additions & 0 deletions LeetcodeProblems/Swap_Nodes_In_Pairs.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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/ |
| [Coin Change ](/LeetcodeProblems/Coin_Change.js) | Medium | https://leetcode.com/problems/coin-change/ |
Expand Down