Skip to content

Commit d7cda53

Browse files
Run Prettier Batch 2
1 parent 1ca670c commit d7cda53

10 files changed

+341
-310
lines changed

LeetcodeProblems/Algorithms/Clone_Graph.js

+38-41
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

+42-39
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

+40-17
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;

0 commit comments

Comments
 (0)