Skip to content

Commit fdd07f9

Browse files
Merge pull request ignacio-chiazzo#28 from ignacio-chiazzo/prob
Prob
2 parents 6f61fe9 + 94b554c commit fdd07f9

File tree

5 files changed

+172
-1
lines changed

5 files changed

+172
-1
lines changed

DesignDataStructure/designI.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Design an efficient data structure for given operations
3+
Design a Data Structure for the following operations.
4+
The data structure should be efficient enough to accommodate the operations according to their frequency.
5+
1) findMin() : Returns the minimum item.
6+
Frequency: Most frequent
7+
8+
2) findMax() : Returns the maximum item.
9+
Frequency: Most frequent
10+
11+
3) deleteMin() : Delete the minimum item.
12+
Frequency: Moderate frequent
13+
14+
4) deleteMax() : Delete the maximum item.
15+
Frequency: Moderate frequent
16+
17+
5) Insert() : Inserts an item.
18+
Frequency: Least frequent
19+
20+
6) Delete() : Deletes an item.
21+
Frequency: Least frequent.
22+
*/
23+
24+
25+
class effStructure {
26+
this.maxHeap = [];
27+
this.minHeap = [];
28+
}
29+
30+
31+
1) findMin(): O(1)
32+
2) findMax(): O(1)
33+
3) deleteMin(): O(log N)
34+
4) deleteMax(): O(log N)
35+
5) Insert(log N)
36+
6) Delete: O(log N)

LeetcodeProblems/Number_of_Islands.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Number of Islands
3+
https://leetcode.com/problems/number-of-islands/
4+
5+
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.
6+
7+
Example 1:
8+
9+
Input:
10+
11110
11+
11010
12+
11000
13+
00000
14+
15+
Output: 1
16+
Example 2:
17+
18+
Input:
19+
11000
20+
11000
21+
00100
22+
00011
23+
24+
Output: 3
25+
*/
26+
27+
/*
28+
* @param {character[][]} grid
29+
* @return {number}
30+
*/
31+
var numIslands = function(grid) {
32+
if(grid.length === 0)
33+
return 0;
34+
35+
var countIslands = 0;
36+
const rowsCount = grid.length;
37+
const columnsCount = grid[0].length;
38+
for(var i = 0; i < rowsCount; i++) {
39+
for(var j = 0; j < columnsCount; j++) {
40+
if(grid[i][j] == 1) {
41+
countIslands++;
42+
colorIsland(grid, i, j, rowsCount, columnsCount);
43+
}
44+
}
45+
}
46+
47+
return countIslands;
48+
};
49+
50+
var colorIsland = function(grid, i, j, rowsCount, columnsCount) {
51+
if(i < 0 || j < 0 || i >= rowsCount || j >= columnsCount || grid[i][j] == 0)
52+
return;
53+
54+
grid[i][j] = 0;
55+
56+
colorIsland(grid, i - 1, j, rowsCount, columnsCount);
57+
colorIsland(grid, i + 1, j, rowsCount, columnsCount);
58+
colorIsland(grid, i, j - 1, rowsCount, columnsCount);
59+
colorIsland(grid, i, j + 1, rowsCount, columnsCount);
60+
}
61+
62+
var main = function() {
63+
console.log(numIslands([[1]]));
64+
console.log(numIslands([]));
65+
console.log(numIslands(
66+
[
67+
["1","1","1","1","0"],
68+
["1","1","0","1","0"],
69+
["1","1","0","0","0"],
70+
["0","0","0","0","0"]
71+
])
72+
);
73+
}
74+
75+
module.exports.main = main;

LeetcodeProblems/Permutations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ var main = function() {
4545
console.log(permute([1,2,3,4,5,6]));
4646
}
4747

48-
exports.module.main = main;
48+
module.exports.main = main;
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Swap Nodes in Pairs
3+
https://leetcode.com/problems/swap-nodes-in-pairs/
4+
5+
Given a linked list, swap every two adjacent nodes and return its head.
6+
7+
Example:
8+
9+
Given 1->2->3->4, you should return the list as 2->1->4->3.
10+
Note:
11+
12+
Your algorithm should use only constant extra space.
13+
You may not modify the values in the list's nodes, only nodes itself may be changed.
14+
*/
15+
16+
var ListNode = require('../UtilsClasses/ListNode').ListNode;
17+
18+
/**
19+
* Definition for singly-linked list.
20+
* function ListNode(val) {
21+
* this.val = val;
22+
* this.next = null;
23+
* }
24+
*/
25+
/**
26+
* @param {ListNode} head
27+
* @return {ListNode}
28+
*/
29+
var swapPairs = function(head) {
30+
if(head === null || head.next === null)
31+
return head
32+
var previous = null;
33+
var current = head;
34+
var following = (head.next != null) ? head.next.next : null;
35+
head = head.next;
36+
37+
while(current !== null && current.next !== null) {
38+
var next = current.next;
39+
next.next = current;
40+
if(previous != null)
41+
previous.next = next;
42+
current.next = following;
43+
previous = current;
44+
current = following;
45+
following = (current !== null && current.next != null) ? current.next.next : null;
46+
}
47+
48+
return head;
49+
};
50+
51+
var main = function() {
52+
console.log(swapPairs(ListNode.linkenList([1,2,3,4])));
53+
console.log(swapPairs(ListNode.linkenList([])));
54+
console.log(swapPairs(ListNode.linkenList([1])));
55+
console.log(swapPairs(ListNode.linkenList([1,2])));
56+
}
57+
58+
module.exports.main = main;

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Solutions of algorithm problems using Javascript
1414
| [merge k sorted lists ](/LeetcodeProblems/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ |
1515
| [Subarray Sum Equals K ](/LeetcodeProblems/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ |
1616
| [3Sum ](/LeetcodeProblems/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ |
17+
| [NumberOfIslands ](/LeetcodeProblems/Number_of_Islands.js) | Medium | https://leetcode.com/problems/number-of-islands/ |
18+
| [Swap Nodes in Pairs](/LeetcodeProblems/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ |
1719
| [Add Two Numbers ](/LeetcodeProblems/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ |
1820
| [Clone Graph ](/LeetcodeProblems/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ |
1921
| [Coin Change ](/LeetcodeProblems/Coin_Change.js) | Medium | https://leetcode.com/problems/coin-change/ |

0 commit comments

Comments
 (0)