Skip to content

Commit d7cda53

Browse files
committedMay 11, 2022
Run Prettier Batch 2
1 parent 1ca670c commit d7cda53

10 files changed

+341
-310
lines changed
 

‎LeetcodeProblems/Algorithms/Clone_Graph.js

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,54 +45,51 @@ You don't need to understand the serialization to solve the problem.
4545
* @param {UndirectedGraphNode} graph
4646
* @return {UndirectedGraphNode}
4747
*/
48-
var cloneGraph = function(graph) {
49-
if(!graph)
50-
return graph;
51-
48+
var cloneGraph = function (graph) {
49+
if (!graph) return graph;
50+
5251
return dfs(graph, {});
5352
};
5453

55-
var dfs = function(graph, visited) {
56-
if(visited[graph.label])
57-
return visited[graph.label];
58-
54+
var dfs = function (graph, visited) {
55+
if (visited[graph.label]) return visited[graph.label];
56+
5957
var newNode = new UndirectedGraphNode(graph.label);
6058
visited[newNode.label] = newNode;
61-
62-
for(var i = 0; i < graph.neighbors.length; i++) {
63-
const neighbor = dfs(graph.neighbors[i], visited);
64-
newNode.neighbors.push(neighbor);
59+
60+
for (var i = 0; i < graph.neighbors.length; i++) {
61+
const neighbor = dfs(graph.neighbors[i], visited);
62+
newNode.neighbors.push(neighbor);
6563
}
66-
64+
6765
return newNode;
68-
}
66+
};
6967

7068
// SOLUTION 2 Using DFS
71-
var cloneGraphBFS = function(graph) {
72-
if(graph === null)
73-
return graph;
74-
75-
var visitedMap = {};
76-
var queue = [graph];
77-
var copyReturn = new UndirectedGraphNode(graph.label);
78-
visitedMap[graph.label] = copyReturn;
79-
80-
while(queue.length > 0) {
81-
var node = queue.shift();
82-
var nodeCopied = visitedMap[node.label];
83-
84-
for(var i = 0; i < node.neighbors.length; i++) {
85-
var neighbor = node.neighbors[i];
86-
87-
if(!visitedMap[neighbor.label]) {
88-
var copyNeighbor = new UndirectedGraphNode(neighbor.label);
89-
visitedMap[neighbor.label] = copyNeighbor;
90-
queue.push(neighbor);
91-
}
92-
93-
nodeCopied.neighbors.push(visitedMap[neighbor.label]);
94-
}
69+
var cloneGraphBFS = function (graph) {
70+
if (graph === null) return graph;
71+
72+
var visitedMap = {};
73+
var queue = [graph];
74+
var copyReturn = new UndirectedGraphNode(graph.label);
75+
visitedMap[graph.label] = copyReturn;
76+
77+
while (queue.length > 0) {
78+
var node = queue.shift();
79+
var nodeCopied = visitedMap[node.label];
80+
81+
for (var i = 0; i < node.neighbors.length; i++) {
82+
var neighbor = node.neighbors[i];
83+
84+
if (!visitedMap[neighbor.label]) {
85+
var copyNeighbor = new UndirectedGraphNode(neighbor.label);
86+
visitedMap[neighbor.label] = copyNeighbor;
87+
queue.push(neighbor);
88+
}
89+
90+
nodeCopied.neighbors.push(visitedMap[neighbor.label]);
9591
}
96-
97-
return copyReturn;
98-
}
92+
}
93+
94+
return copyReturn;
95+
};

‎LeetcodeProblems/Algorithms/Coin_Change.js

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,86 +19,89 @@ Note:
1919
You may assume that you have an infinite number of each kind of coin.
2020
*/
2121

22-
// Solution 3
23-
var coinChange = function(coins, amount) {
22+
// Solution 3
23+
var coinChange = function (coins, amount) {
2424
var memo = [];
25-
for(var i = 0; i <= amount; i++)
26-
memo[i] = Number.POSITIVE_INFINITY;
25+
for (var i = 0; i <= amount; i++) memo[i] = Number.POSITIVE_INFINITY;
2726

2827
memo[0] = 0;
29-
for(var i = 0; i < coins.length; i++) {
28+
for (var i = 0; i < coins.length; i++) {
3029
const coin = coins[i];
31-
for(var j = coin; j < memo.length; j++)
30+
for (var j = coin; j < memo.length; j++)
3231
memo[j] = min2(memo[j], memo[j - coin] + 1);
3332
}
3433

35-
return (memo[amount] == Number.POSITIVE_INFINITY) ? -1 : memo[amount];
34+
return memo[amount] == Number.POSITIVE_INFINITY ? -1 : memo[amount];
3635
};
3736

38-
var min2 = function(a, b) {
39-
return (a < b) ? a : b;
40-
}
37+
var min2 = function (a, b) {
38+
return a < b ? a : b;
39+
};
4140

4241
// Solution 2
43-
var buildMemoKey = function(position, amount) {
42+
var buildMemoKey = function (position, amount) {
4443
return position + "-" + amount;
45-
}
44+
};
4645

47-
var coinChange2 = function(coins, amount) {
46+
var coinChange2 = function (coins, amount) {
4847
var memo = {};
49-
var solution = coinChangeAux2(coins, amount, 0, memo, Number.POSITIVE_INFINITY);
48+
var solution = coinChangeAux2(
49+
coins,
50+
amount,
51+
0,
52+
memo,
53+
Number.POSITIVE_INFINITY
54+
);
5055
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
5156
};
5257

53-
var coinChangeAux2 = function(coins, amount, pos, memo) {
58+
var coinChangeAux2 = function (coins, amount, pos, memo) {
5459
var key = buildMemoKey(pos, amount);
55-
if(memo[key])
56-
return memo[key];
57-
58-
if(amount < 0) {
59-
return Number.POSITIVE_INFINITY;
60-
} else if(amount == 0) {
60+
if (memo[key]) return memo[key];
61+
62+
if (amount < 0) {
63+
return Number.POSITIVE_INFINITY;
64+
} else if (amount == 0) {
6165
return 0;
62-
} else if(pos >= coins.length) {
66+
} else if (pos >= coins.length) {
6367
return Number.POSITIVE_INFINITY;
6468
}
65-
69+
6670
var left = coinChangeAux2(coins, amount, pos + 1, memo);
6771
var middle = 1 + coinChangeAux2(coins, amount - coins[pos], pos + 1, memo);
6872
var right = 1 + coinChangeAux2(coins, amount - coins[pos], pos, memo);
69-
73+
7074
var solution = min(left, middle, right);
7175
memo[key] = solution;
7276
return solution;
73-
}
77+
};
7478

7579
// Solution 1 naive
76-
var coinChange1 = function(coins, amount) {
80+
var coinChange1 = function (coins, amount) {
7781
var solution = coinChangeAux1(coins, amount, 0);
7882
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
7983
};
8084

81-
var coinChangeAux1 = function(coins, amount, pos) {
82-
if(amount < 0) {
83-
return Number.POSITIVE_INFINITY;
84-
} else if(amount == 0) {
85+
var coinChangeAux1 = function (coins, amount, pos) {
86+
if (amount < 0) {
87+
return Number.POSITIVE_INFINITY;
88+
} else if (amount == 0) {
8589
return 0;
86-
} else if(pos >= coins.length) {
90+
} else if (pos >= coins.length) {
8791
return Number.POSITIVE_INFINITY;
8892
}
89-
93+
9094
var left = coinChangeAux1(coins, amount, pos + 1);
9195
var middle = 1 + coinChangeAux1(coins, amount - coins[pos], pos + 1);
9296
var right = 1 + coinChangeAux1(coins, amount - coins[pos], pos);
93-
97+
9498
var partialSol = min(left, middle, right);
9599
return partialSol;
96-
}
100+
};
97101

98-
var min = function(a, b, c) {
99-
if(a < b)
100-
return (a < c) ? a : c;
101-
return (b < c) ? b : c;
102-
}
102+
var min = function (a, b, c) {
103+
if (a < b) return a < c ? a : c;
104+
return b < c ? b : c;
105+
};
103106

104107
module.exports.coinChange = coinChange;

‎LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,57 @@ Return the following binary tree:
2727
* this.left = this.right = null;
2828
* }
2929
*/
30-
var TreeNode = require('../../UtilsClasses/TreeNode').TreeNode;
30+
var TreeNode = require("../../UtilsClasses/TreeNode").TreeNode;
3131

3232
/**
3333
* @param {number[]} preorder
3434
* @param {number[]} inorder
3535
* @return {TreeNode}
3636
*/
37-
var buildTree = function(preorder, inorder) {
38-
if(preorder === null || inorder === null || preorder.length !== inorder.length)
37+
var buildTree = function (preorder, inorder) {
38+
if (
39+
preorder === null ||
40+
inorder === null ||
41+
preorder.length !== inorder.length
42+
)
3943
return nil;
40-
41-
return buildTreeAux(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
44+
45+
return buildTreeAux(
46+
preorder,
47+
0,
48+
preorder.length - 1,
49+
inorder,
50+
0,
51+
inorder.length - 1
52+
);
4253
};
4354

44-
var buildTreeAux = function(preorder, pl, ph, inorder, il, ih) {
45-
if(pl > ph || il > ih)
46-
return null;
47-
55+
var buildTreeAux = function (preorder, pl, ph, inorder, il, ih) {
56+
if (pl > ph || il > ih) return null;
57+
4858
const rootValue = preorder[pl];
4959
var countElementsLeft = 0;
50-
while(inorder[il + countElementsLeft] !== rootValue)
51-
countElementsLeft++;
52-
60+
while (inorder[il + countElementsLeft] !== rootValue) countElementsLeft++;
61+
5362
var ret = new TreeNode(rootValue);
54-
ret.left = buildTreeAux(preorder, pl + 1, pl + countElementsLeft, inorder, il, il + countElementsLeft - 1);
55-
ret.right = buildTreeAux(preorder, pl + countElementsLeft + 1, ph, inorder, il + countElementsLeft + 1, ih);
56-
63+
ret.left = buildTreeAux(
64+
preorder,
65+
pl + 1,
66+
pl + countElementsLeft,
67+
inorder,
68+
il,
69+
il + countElementsLeft - 1
70+
);
71+
ret.right = buildTreeAux(
72+
preorder,
73+
pl + countElementsLeft + 1,
74+
ph,
75+
inorder,
76+
il + countElementsLeft + 1,
77+
ih
78+
);
79+
5780
return ret;
58-
}
81+
};
5982

60-
module.exports.buildTree = buildTree
83+
module.exports.buildTree = buildTree;

‎LeetcodeProblems/Algorithms/Deletion_Distance.js

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,91 +20,95 @@ output: 0
2020
*/
2121

2222
// Solution 3 Using DP
23-
var deletionDistanceDP = function(str1, str2) {
24-
if(str1.length === 0)
25-
return str2.length;
26-
if(str2.length === 0)
27-
return str1.length;
23+
var deletionDistanceDP = function (str1, str2) {
24+
if (str1.length === 0) return str2.length;
25+
if (str2.length === 0) return str1.length;
2826

2927
var matrix = [];
30-
for(var i = 0; i <= str1.length; i++) {
28+
for (var i = 0; i <= str1.length; i++) {
3129
matrix[i] = [];
32-
for(var j = 0; j <= str2.length; j++) {
33-
if(i === 0) {
30+
for (var j = 0; j <= str2.length; j++) {
31+
if (i === 0) {
3432
matrix[i][j] = j;
35-
} else if(j == 0) {
33+
} else if (j == 0) {
3634
matrix[i][j] = i;
37-
} else if(str1[i - 1] === str2[j - 1]) {
38-
matrix[i][j] = matrix[i - 1][j - 1];
35+
} else if (str1[i - 1] === str2[j - 1]) {
36+
matrix[i][j] = matrix[i - 1][j - 1];
3937
} else {
4038
matrix[i][j] = 1 + min(matrix[i - 1][j], matrix[i][j - 1]);
4139
}
4240
}
4341
}
44-
42+
4543
return matrix[str1.length][str2.length];
46-
}
44+
};
4745

4846
// Solution 2 Using memoization
49-
var deletionDistance2 = function(str1, str2) {
47+
var deletionDistance2 = function (str1, str2) {
5048
var memo = {};
5149
return deletionDistanceAux2(str1, str2, 0, 0, memo);
52-
}
50+
};
5351

54-
var deletionDistanceAux2 = function(str1, str2, pos1, pos2, memo) {
52+
var deletionDistanceAux2 = function (str1, str2, pos1, pos2, memo) {
5553
const valueCashed = getValue(pos1, pos2, memo);
56-
if(valueCashed !== undefined)
57-
return valueCashed;
54+
if (valueCashed !== undefined) return valueCashed;
5855
var result;
5956

60-
if(str1.length === pos1)
61-
result = str2.length - pos2;
62-
else if(str2.length === pos2)
63-
result = str1.length - pos1;
64-
else if(str1[pos1] === str2[pos2])
57+
if (str1.length === pos1) result = str2.length - pos2;
58+
else if (str2.length === pos2) result = str1.length - pos1;
59+
else if (str1[pos1] === str2[pos2])
6560
result = deletionDistanceAux2(str1, str2, pos1 + 1, pos2 + 1, memo);
66-
else
67-
result = 1 + min(deletionDistanceAux2(str1, str2, pos1, pos2 + 1, memo), deletionDistanceAux2(str1, str2, pos1 + 1, pos2, memo))
61+
else
62+
result =
63+
1 +
64+
min(
65+
deletionDistanceAux2(str1, str2, pos1, pos2 + 1, memo),
66+
deletionDistanceAux2(str1, str2, pos1 + 1, pos2, memo)
67+
);
6868

6969
return setValue(pos1, pos2, result, memo);
70-
}
70+
};
7171

72-
var getMemoKey = function(pos1, pos2) {
72+
var getMemoKey = function (pos1, pos2) {
7373
return pos1 + "-" + pos2;
74-
}
74+
};
7575

76-
var getValue = function(pos1, pos2, memo) {
76+
var getValue = function (pos1, pos2, memo) {
7777
const memoKey = getMemoKey(pos1, pos2);
7878
return memo[memoKey];
79-
}
79+
};
8080

81-
var setValue = function(pos1, pos2, value, memo) {
81+
var setValue = function (pos1, pos2, value, memo) {
8282
const memoKey = getMemoKey(pos1, pos2);
8383
memo[memoKey] = value;
8484
return value;
85-
}
85+
};
8686

8787
// Solution 1 naive
88-
var deletionDistance = function(str1, str2) {
88+
var deletionDistance = function (str1, str2) {
8989
return deletionDistanceAux(str1, str2, 0, 0);
90-
}
91-
92-
var deletionDistanceAux = function(str1, str2, pos1, pos2) {
93-
if(str1.length === pos1)
94-
return str2.length - pos2;
95-
96-
if(str2.length === pos2)
97-
return str1.length - pos1;
98-
99-
if(str1[pos1] === str2[pos2])
100-
return deletionDistanceAux(str1, str2, pos1 + 1, pos2 + 1);
90+
};
91+
92+
var deletionDistanceAux = function (str1, str2, pos1, pos2) {
93+
if (str1.length === pos1) return str2.length - pos2;
10194

102-
return 1 + min(deletionDistanceAux(str1, str2, pos1, pos2 + 1), deletionDistanceAux(str1, str2, pos1 + 1, pos2));
103-
}
95+
if (str2.length === pos2) return str1.length - pos1;
96+
97+
if (str1[pos1] === str2[pos2])
98+
return deletionDistanceAux(str1, str2, pos1 + 1, pos2 + 1);
10499

105-
var min = function(a, b) {
106-
return (a < b) ? a : b;
107-
}
100+
return (
101+
1 +
102+
min(
103+
deletionDistanceAux(str1, str2, pos1, pos2 + 1),
104+
deletionDistanceAux(str1, str2, pos1 + 1, pos2)
105+
)
106+
);
107+
};
108+
109+
var min = function (a, b) {
110+
return a < b ? a : b;
111+
};
108112

109113
module.exports.deletionDistance = deletionDistance;
110114
module.exports.deletionDistance2 = deletionDistance2;

‎LeetcodeProblems/Algorithms/Design_Circular_Deque.js

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,94 +40,88 @@ Please do not use the built-in Deque library.
4040
* Initialize your data structure here. Set the size of the deque to be k.
4141
* @param {number} k
4242
*/
43-
var MyCircularDeque = function(k) {
43+
var MyCircularDeque = function (k) {
4444
this.queue = [];
4545
this.maxSize = k;
4646
};
4747

4848
/**
49-
* Adds an item at the front of Deque. Return true if the operation is successful.
50-
* @param {number} value
51-
* @return {boolean}
52-
*/
53-
MyCircularDeque.prototype.insertFront = function(value) {
54-
if(this.isFull())
55-
return false;
56-
57-
this.queue.unshift(value);
49+
* Adds an item at the front of Deque. Return true if the operation is successful.
50+
* @param {number} value
51+
* @return {boolean}
52+
*/
53+
MyCircularDeque.prototype.insertFront = function (value) {
54+
if (this.isFull()) return false;
55+
56+
this.queue.unshift(value);
5857
return true;
5958
};
6059

6160
/**
62-
* Adds an item at the rear of Deque. Return true if the operation is successful.
63-
* @param {number} value
64-
* @return {boolean}
65-
*/
66-
MyCircularDeque.prototype.insertLast = function(value) {
67-
if(this.isFull())
68-
return false;
69-
61+
* Adds an item at the rear of Deque. Return true if the operation is successful.
62+
* @param {number} value
63+
* @return {boolean}
64+
*/
65+
MyCircularDeque.prototype.insertLast = function (value) {
66+
if (this.isFull()) return false;
67+
7068
this.queue[this.queue.length] = value;
7169
return true;
7270
};
7371

7472
/**
75-
* Deletes an item from the front of Deque. Return true if the operation is successful.
76-
* @return {boolean}
77-
*/
78-
MyCircularDeque.prototype.deleteFront = function() {
79-
if(this.isEmpty())
80-
return false;
81-
73+
* Deletes an item from the front of Deque. Return true if the operation is successful.
74+
* @return {boolean}
75+
*/
76+
MyCircularDeque.prototype.deleteFront = function () {
77+
if (this.isEmpty()) return false;
78+
8279
this.queue.shift(1);
8380
return true;
8481
};
8582

8683
/**
87-
* Deletes an item from the rear of Deque. Return true if the operation is successful.
88-
* @return {boolean}
89-
*/
90-
MyCircularDeque.prototype.deleteLast = function() {
91-
if(this.isEmpty())
92-
return false;
93-
84+
* Deletes an item from the rear of Deque. Return true if the operation is successful.
85+
* @return {boolean}
86+
*/
87+
MyCircularDeque.prototype.deleteLast = function () {
88+
if (this.isEmpty()) return false;
89+
9490
this.queue.splice(this.queue.length - 1, 1);
9591
return true;
9692
};
9793

9894
/**
99-
* Get the front item from the deque.
100-
* @return {number}
101-
*/
102-
MyCircularDeque.prototype.getFront = function() {
103-
if(this.isEmpty())
104-
return -1;
95+
* Get the front item from the deque.
96+
* @return {number}
97+
*/
98+
MyCircularDeque.prototype.getFront = function () {
99+
if (this.isEmpty()) return -1;
105100
return this.queue[0];
106101
};
107102

108103
/**
109-
* Get the last item from the deque.
110-
* @return {number}
111-
*/
112-
MyCircularDeque.prototype.getRear = function() {
113-
if(this.isEmpty())
114-
return -1;
104+
* Get the last item from the deque.
105+
* @return {number}
106+
*/
107+
MyCircularDeque.prototype.getRear = function () {
108+
if (this.isEmpty()) return -1;
115109
return this.queue[this.queue.length - 1];
116110
};
117111

118112
/**
119-
* Checks whether the circular deque is empty or not.
120-
* @return {boolean}
121-
*/
122-
MyCircularDeque.prototype.isEmpty = function() {
113+
* Checks whether the circular deque is empty or not.
114+
* @return {boolean}
115+
*/
116+
MyCircularDeque.prototype.isEmpty = function () {
123117
return this.queue.length === 0;
124118
};
125119

126120
/**
127-
* Checks whether the circular deque is full or not.
128-
* @return {boolean}
129-
*/
130-
MyCircularDeque.prototype.isFull = function() {
121+
* Checks whether the circular deque is full or not.
122+
* @return {boolean}
123+
*/
124+
MyCircularDeque.prototype.isFull = function () {
131125
return this.queue.length === this.maxSize;
132126
};
133127

‎LeetcodeProblems/Algorithms/Edit_Distance.js

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,72 +30,71 @@ exection -> execution (insert 'u')
3030
*/
3131

3232
// Optimal solution
33-
var minDistance = function(word1, word2) {
33+
var minDistance = function (word1, word2) {
3434
var matrix = [];
35-
for(var i = 0; i <= word1.length; i++) {
35+
for (var i = 0; i <= word1.length; i++) {
3636
matrix[i] = [];
37-
for(var j = 0; j <= word2.length; j++) {
38-
if(i === 0)
39-
matrix[i][j] = j;
40-
else if(j === 0)
41-
matrix[i][j] = i;
42-
else
43-
matrix[i][j] = 0;
37+
for (var j = 0; j <= word2.length; j++) {
38+
if (i === 0) matrix[i][j] = j;
39+
else if (j === 0) matrix[i][j] = i;
40+
else matrix[i][j] = 0;
4441
}
45-
};
46-
47-
for(var i = 1; i <= word1.length; i++) {
48-
for(var j = 1; j <= word2.length; j++) {
49-
if(word1.charAt(i - 1) === word2.charAt(j - 1)) {
42+
}
43+
44+
for (var i = 1; i <= word1.length; i++) {
45+
for (var j = 1; j <= word2.length; j++) {
46+
if (word1.charAt(i - 1) === word2.charAt(j - 1)) {
5047
matrix[i][j] = matrix[i - 1][j - 1];
5148
} else {
52-
matrix[i][j] = 1 + min(
53-
matrix[i - 1][j - 1],
54-
matrix[i - 1][j], // add
55-
matrix[i][j - 1] // remove
56-
);
49+
matrix[i][j] =
50+
1 +
51+
min(
52+
matrix[i - 1][j - 1],
53+
matrix[i - 1][j], // add
54+
matrix[i][j - 1] // remove
55+
);
5756
}
5857
}
5958
}
60-
59+
6160
return matrix[word1.length][word2.length];
6261
};
6362

64-
var min = function(a, b, c) {
65-
if(a < b) {
66-
return (a < c) ? a : c;
63+
var min = function (a, b, c) {
64+
if (a < b) {
65+
return a < c ? a : c;
6766
}
68-
return (b < c) ? b : c;
69-
}
67+
return b < c ? b : c;
68+
};
7069

7170
//Solution 2
72-
var minDistance2 = function(word1, word2) {
71+
var minDistance2 = function (word1, word2) {
7372
return minDistanceAux(word1, word2, 0, 0);
74-
}
75-
76-
var minDistanceAux = function(word1, word2, iter1, iter2) {
77-
if(word1.length === iter1)
78-
return word2.length - iter2;
79-
80-
if(word2.length === iter2)
81-
return word1.length - iter1;
82-
83-
if(word1.charAt(iter1) === word2.charAt(iter2))
73+
};
74+
75+
var minDistanceAux = function (word1, word2, iter1, iter2) {
76+
if (word1.length === iter1) return word2.length - iter2;
77+
78+
if (word2.length === iter2) return word1.length - iter1;
79+
80+
if (word1.charAt(iter1) === word2.charAt(iter2))
8481
return minDistanceAux(word1, word2, iter1 + 1, iter2 + 1);
85-
86-
return 1 + min(
87-
minDistanceAux(word1, word2, iter1 + 1, iter2 + 1),
88-
minDistanceAux(word1, word2, iter1, iter2 + 1), // add
89-
minDistanceAux(word1, word2, iter1 + 1, iter2 ) // delete
90-
)
82+
83+
return (
84+
1 +
85+
min(
86+
minDistanceAux(word1, word2, iter1 + 1, iter2 + 1),
87+
minDistanceAux(word1, word2, iter1, iter2 + 1), // add
88+
minDistanceAux(word1, word2, iter1 + 1, iter2) // delete
89+
)
90+
);
9191
};
9292

93-
var min = function(a, b, c) {
94-
if(a < b)
95-
return (a < c) ? a : c;
96-
97-
return (b < c) ? b : c;
98-
}
93+
var min = function (a, b, c) {
94+
if (a < b) return a < c ? a : c;
95+
96+
return b < c ? b : c;
97+
};
9998

10099
module.exports.minDistance = minDistance;
101100
module.exports.minDistance2 = minDistance2;

‎LeetcodeProblems/Algorithms/Escape_The_Ghosts.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,20 @@ The number of ghosts will not exceed 100.
4242
* @param {number[]} target
4343
* @return {boolean}
4444
*/
45-
var escapeGhosts = function(ghosts, target) {
46-
var distancePacman = getDistance([0,0], target);
47-
for(ghost in ghosts) {
45+
var escapeGhosts = function (ghosts, target) {
46+
var distancePacman = getDistance([0, 0], target);
47+
for (ghost in ghosts) {
4848
const distanceGhost = getDistance(ghosts[ghost], target);
49-
if(distancePacman > distanceGhost)
50-
return false
49+
if (distancePacman > distanceGhost) return false;
5150
}
52-
51+
5352
return true;
5453
};
5554

56-
var getDistance = function(a, b) {
55+
var getDistance = function (a, b) {
5756
const horizontalMoves = Math.abs(a[0] - b[0]);
5857
const verticalMoves = Math.abs(a[1] - b[1]);
5958
return horizontalMoves + verticalMoves;
60-
}
59+
};
6160

6261
module.exports.escapeGhosts = escapeGhosts;

‎LeetcodeProblems/Algorithms/Flood_Fill.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,25 @@ The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image
2929
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
3030
*/
3131

32-
var floodFill = function(image, sr, sc, newColor) {
32+
var floodFill = function (image, sr, sc, newColor) {
3333
var oldColor = image[sr][sc];
34-
35-
if(newColor == oldColor)
36-
return image;
37-
34+
35+
if (newColor == oldColor) return image;
36+
3837
image[sr][sc] = newColor;
39-
40-
if(sr > 0 && image[sr - 1][sc] == oldColor)
38+
39+
if (sr > 0 && image[sr - 1][sc] == oldColor)
4140
floodFill(image, sr - 1, sc, newColor); //Left
42-
43-
if(sc > 0 && image[sr][sc - 1] == oldColor)
41+
42+
if (sc > 0 && image[sr][sc - 1] == oldColor)
4443
floodFill(image, sr, sc - 1, newColor); //Up
4544

46-
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor)
45+
if (sr < image.length - 1 && image[sr + 1][sc] == oldColor)
4746
floodFill(image, sr + 1, sc, newColor); //Down
48-
49-
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor)
47+
48+
if (sc < image[0].length - 1 && image[sr][sc + 1] == oldColor)
5049
floodFill(image, sr, sc + 1, newColor); // Right
51-
50+
5251
return image;
5352
};
5453

‎LeetcodeProblems/Algorithms/Generate_Parenthesis.js

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,81 @@ For example, given n = 3, a solution set is:
1616
*/
1717

1818
// ************************************************ Approach1 ************************************************
19-
var generateParenthesisApproach1 = function(n) {
20-
if(n === 0)
21-
return [];
19+
var generateParenthesisApproach1 = function (n) {
20+
if (n === 0) return [];
2221

2322
var str = "(".repeat(n);
24-
sol = [];
25-
26-
genParAux(str, 0, 0, sol)
23+
sol = [];
24+
25+
genParAux(str, 0, 0, sol);
2726
return sol;
2827
};
2928

30-
var genParAux = function(str, position, leftParentheses, sol) {
31-
if(position === str.length) {
29+
var genParAux = function (str, position, leftParentheses, sol) {
30+
if (position === str.length) {
3231
var ret = str + ")".repeat(leftParentheses);
3332
sol.push(ret);
3433
return;
3534
}
3635

3736
genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything
38-
if(leftParentheses === 0)
39-
return;
37+
if (leftParentheses === 0) return;
4038

41-
for(var i = 1; i <= leftParentheses; i++) {
39+
for (var i = 1; i <= leftParentheses; i++) {
4240
var parString = ")".repeat(i);
4341
var partSol = str.slice(0, position) + parString + str.slice(position); // Insert i parentheses in the position
4442
genParAux(partSol, position + i + 1, leftParentheses - i + 1, sol);
45-
}
46-
}
43+
}
44+
};
4745

4846
// ************************************************ Approach2 ************************************************
49-
var generateParenthesisApproach2 = function(n) {
50-
if(n === 0)
51-
return [];
47+
var generateParenthesisApproach2 = function (n) {
48+
if (n === 0) return [];
5249

5350
var sol = [];
54-
genParAuxApproach2("", 0, 0, 0, n * 2, sol)
51+
genParAuxApproach2("", 0, 0, 0, n * 2, sol);
5552
return sol;
56-
}
53+
};
54+
55+
var genParAuxApproach2 = function (
56+
str,
57+
leftPar,
58+
rightPar,
59+
index,
60+
totalCharCount,
61+
sol
62+
) {
63+
if (index === totalCharCount) {
64+
if (rightPar === leftPar) sol.push(str);
5765

58-
var genParAuxApproach2 = function(str, leftPar, rightPar, index, totalCharCount, sol) {
59-
if(index === totalCharCount) {
60-
if(rightPar === leftPar)
61-
sol.push(str);
62-
6366
return;
6467
}
6568

6669
var strLeft = insertAt(str, index, "(");
67-
genParAuxApproach2(strLeft, leftPar + 1, rightPar, index + 1, totalCharCount, sol);
70+
genParAuxApproach2(
71+
strLeft,
72+
leftPar + 1,
73+
rightPar,
74+
index + 1,
75+
totalCharCount,
76+
sol
77+
);
78+
79+
if (rightPar === leftPar) return;
6880

69-
if(rightPar === leftPar)
70-
return;
71-
7281
var strRight = insertAt(str, index, ")");
73-
genParAuxApproach2(strRight, leftPar, rightPar + 1, index + 1, totalCharCount, sol);
74-
}
82+
genParAuxApproach2(
83+
strRight,
84+
leftPar,
85+
rightPar + 1,
86+
index + 1,
87+
totalCharCount,
88+
sol
89+
);
90+
};
7591

76-
var insertAt = function(str, position, value) {
92+
var insertAt = function (str, position, value) {
7793
return str.slice(0, position) + value + str.slice(position);
78-
}
94+
};
7995

8096
module.exports.generateParenthesisApproach2 = generateParenthesisApproach2;

‎LeetcodeProblems/Algorithms/Group_Anagrams.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,29 @@ All inputs will be in lowercase.
1919
The order of your output does not matter.
2020
*/
2121

22-
var groupAnagrams = function(strs) {
22+
var groupAnagrams = function (strs) {
2323
var ret = [];
2424
var hashMap = {};
25-
for(var i = 0; i < strs.length; i++) {
25+
for (var i = 0; i < strs.length; i++) {
2626
const elem = strs[i];
2727
const elemSorted = sortString(strs[i]);
28-
29-
if(hashMap[elemSorted]) {
28+
29+
if (hashMap[elemSorted]) {
3030
hashMap[elemSorted].push(elem);
3131
} else {
3232
hashMap[elemSorted] = [elem];
3333
}
3434
}
3535

36-
for(key in hashMap)
37-
ret.push(hashMap[key]);
38-
36+
for (key in hashMap) ret.push(hashMap[key]);
3937

4038
return ret;
4139
};
4240

43-
var sortString = function(str) {
44-
if(str.length === 0)
45-
return str;
46-
41+
var sortString = function (str) {
42+
if (str.length === 0) return str;
43+
4744
return str.split("").sort().join("");
48-
}
45+
};
4946

5047
module.exports.groupAnagrams = groupAnagrams;

0 commit comments

Comments
 (0)
Please sign in to comment.