Skip to content

Commit e1ea6d2

Browse files
Added TreeNode util class
1 parent 85a4ef3 commit e1ea6d2

10 files changed

+90
-98
lines changed

LeetcodeProblems/Add_Two_Numbers.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,37 @@ var addTwoNumbers = function(l1, l2) {
4444

4545
const head = number;
4646
while(l1 !== null || l2 !== null) {
47-
var l1elem = 0;
48-
var l2elem = 0;
47+
var elem = carry;
4948
if(l1 !== null) {
50-
l1elem = l1.val;
49+
elem += l1.val;
5150
l1 = l1.next;
5251
}
5352
if(l2 !== null) {
54-
l2elem = l2.val;
53+
elem += l2.val;
5554
l2 = l2.next;
5655
}
5756

58-
elem = l1elem + l2elem + carry;
5957
number.next = new ListNode((elem % 10));
6058
number = number.next;
6159
carry = (elem >= 10) ? 1 : 0;
6260
}
63-
if(carry === 1) {
61+
if(carry === 1)
6462
number.next = new ListNode(1);
65-
number = number.next;
66-
}
6763
return head;
6864
};
6965

70-
7166
var main = function() {
7267
const list1 = ListNode.linkenList([1,2,3,4]);
7368
const list2 = ListNode.linkenList([1,2,3,4]);
7469
console.log(addTwoNumbers(list1, list2));
70+
71+
const list3 = ListNode.linkenList([1]);
72+
const list4 = ListNode.linkenList([1,2]);
73+
console.log(addTwoNumbers(list3, list4));
74+
75+
const list5 = ListNode.linkenList([]);
76+
const list6 = ListNode.linkenList([1,2]);
77+
console.log(addTwoNumbers(list5, list6));
7578
}
7679

77-
main();
80+
module.exports.main = main;

LeetcodeProblems/Binary_Gap.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ var binaryGap = function(N) {
5353
currentDist++;
5454
N >>= 1;
5555
}
56-
if(N !== 0 && currentDist > maxDist) {
56+
if(N !== 0 && currentDist > maxDist)
5757
maxDist = currentDist;
58-
}
5958
}
6059
}
6160
return maxDist;
@@ -65,6 +64,5 @@ var main = function() {
6564
console.log(binaryGap(22)); // 10110
6665
console.log(binaryGap(8)); // 1000
6766
}
68-
main();
6967

7068
module.exports.main = main;

LeetcodeProblems/Coin_Change.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Coin Change
33
https://leetcode.com/problems/coin-change/
44
5-
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
5+
You are given coins of different denominations and a total amount of money amount.
6+
Write a function to compute the fewest number of coins that you need to make up that amount.
7+
If that amount of money cannot be made up by any combination of the coins, return -1.
68
79
Example 1:
810
@@ -18,19 +20,16 @@ You may assume that you have an infinite number of each kind of coin.
1820
*/
1921

2022
// Solution 3
21-
var coinChange3 = function(coins, amount) {
23+
var coinChange = function(coins, amount) {
2224
var memo = [];
23-
24-
for(var i = 0; i <= amount; i++) {
25+
for(var i = 0; i <= amount; i++)
2526
memo[i] = Number.POSITIVE_INFINITY;
26-
}
27-
memo[0] = 0;
2827

28+
memo[0] = 0;
2929
for(var i = 0; i < coins.length; i++) {
3030
const coin = coins[i];
31-
for(var j = coin; j < memo.length; j++) {
31+
for(var j = coin; j < memo.length; j++)
3232
memo[j] = min2(memo[j], memo[j - coin] + 1);
33-
}
3433
}
3534

3635
return (memo[amount] == Number.POSITIVE_INFINITY) ? -1 : memo[amount];
@@ -40,7 +39,6 @@ var min2 = function(a, b) {
4039
return (a < b) ? a : b;
4140
}
4241

43-
4442
// Solution 2
4543
var buildMemoKey = function(position, amount) {
4644
return position + "-" + amount;
@@ -54,9 +52,9 @@ var coinChange2 = function(coins, amount) {
5452

5553
var coinChangeAux2 = function(coins, amount, pos, memo) {
5654
var key = buildMemoKey(pos, amount);
57-
if(memo[key]) {
55+
if(memo[key])
5856
return memo[key];
59-
}
57+
6058
if(amount < 0) {
6159
return Number.POSITIVE_INFINITY;
6260
} else if(amount == 0) {
@@ -98,18 +96,19 @@ var coinChangeAux1 = function(coins, amount, pos) {
9896
}
9997

10098
var min = function(a, b, c) {
101-
if(a < b) { return (a < c) ? a : c };
99+
if(a < b)
100+
return (a < c) ? a : c;
102101
return (b < c) ? b : c;
103102
}
104103

105104
function main() {
106105
console.log("-------------");
107-
console.log("Approach 3")
108-
console.log(coinChange3([], 3));
109-
console.log(coinChange3([2], 3));
110-
console.log(coinChange3([1, 2, 5], 11));
111-
console.log(coinChange3([3,7,405,436], 8839));
112-
console.log(coinChange3([370,417,408,156,143,434,168,83,177,280,117], 9953));
106+
console.log("Solution Optimal")
107+
console.log(coinChange([], 3));
108+
console.log(coinChange([2], 3));
109+
console.log(coinChange([1, 2, 5], 11));
110+
console.log(coinChange([3, 7, 405, 436], 8839));
111+
console.log(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953));
113112
}
114113

115114
module.exports.main = main;

LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,29 @@ Return the following binary tree:
2727
* this.left = this.right = null;
2828
* }
2929
*/
30+
31+
var TreeNode = require('../utilsClasses/TreeNode').TreeNode;
32+
3033
/**
3134
* @param {number[]} preorder
3235
* @param {number[]} inorder
3336
* @return {TreeNode}
3437
*/
3538
var buildTree = function(preorder, inorder) {
3639
if(preorder === null || inorder === null || preorder.length !== inorder.length)
37-
return nil;
40+
return nil;
3841

3942
return buildTreeAux(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
4043
};
4144

4245
var buildTreeAux = function(preorder, pl, ph, inorder, il, ih) {
4346
if(pl > ph || il > ih)
44-
return null;
47+
return null;
4548

4649
const rootValue = preorder[pl];
4750
var countElementsLeft = 0;
48-
while(inorder[il + countElementsLeft] !== rootValue) {
51+
while(inorder[il + countElementsLeft] !== rootValue)
4952
countElementsLeft++;
50-
}
5153

5254
var ret = new TreeNode(rootValue);
5355
ret.left = buildTreeAux(preorder, pl + 1, pl + countElementsLeft, inorder, il, il + countElementsLeft - 1);
@@ -56,11 +58,6 @@ var buildTreeAux = function(preorder, pl, ph, inorder, il, ih) {
5658
return ret;
5759
}
5860

59-
var TreeNode = function(val) {
60-
this.val = val;
61-
this.left = this.right = null;
62-
}
63-
6461
var main = function() {
6562
console.log(buildTree([3,9,20,15,7], [9,3,15,20,7]));
6663
}

LeetcodeProblems/Deletion_Distance.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ input: str1 = "", str2 = ""
1919
output: 0
2020
*/
2121

22-
2322
// Solution 3 Using DP
2423
var deletionDistanceDP = function(str1, str2) {
2524
if(str1.length === 0)
@@ -54,9 +53,8 @@ var deletionDistance2 = function(str1, str2) {
5453

5554
var deletionDistanceAux2 = function(str1, str2, pos1, pos2, memo) {
5655
const valueCashed = getValue(pos1, pos2, memo);
57-
if(valueCashed !== undefined) {
56+
if(valueCashed !== undefined)
5857
return valueCashed;
59-
}
6058
var result;
6159

6260
if(str1.length === pos1)
@@ -125,6 +123,4 @@ function main() {
125123
console.log(deletionDistanceDP("", "")); // = 0
126124
}
127125

128-
main();
129-
130126
module.exports.main = main

LeetcodeProblems/Design_Circular_Deque.js

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ getFront(): Gets the front item from the Deque. If the deque is empty, return -1
1515
getRear(): Gets the last item from Deque. If the deque is empty, return -1.
1616
isEmpty(): Checks whether Deque is empty or not.
1717
isFull(): Checks whether Deque is full or not.
18-
1918
2019
Example:
2120
@@ -29,7 +28,6 @@ circularDeque.isFull(); // return true
2928
circularDeque.deleteLast(); // return true
3029
circularDeque.insertFront(4); // return true
3130
circularDeque.getFront(); // return 4
32-
3331
3432
Note:
3533

LeetcodeProblems/Edit_Distance.js

+36-42
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,43 @@ inention -> enention (replace 'i' with 'e')
2727
enention -> exention (replace 'n' with 'x')
2828
exention -> exection (replace 'n' with 'c')
2929
exection -> execution (insert 'u')
30-
3130
*/
3231

3332
// Optimal solution
3433
var minDistance = function(word1, word2) {
3534
var matrix = [];
3635
for(var i = 0; i <= word1.length; i++) {
37-
matrix[i] = [];
38-
for(var j = 0; j <= word2.length; j++) {
39-
if(i === 0) {
40-
matrix[i][j] = j;
41-
} else if(j === 0){
42-
matrix[i][j] = i;
43-
} else {
44-
matrix[i][j] = 0;
45-
}
46-
}
36+
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;
44+
}
4745
};
4846

4947
for(var i = 1; i <= word1.length; i++) {
50-
for(var j = 1; j <= word2.length; j++) {
51-
if(word1.charAt(i - 1) === word2.charAt(j - 1)) {
52-
matrix[i][j] = matrix[i - 1][j - 1];
53-
} else {
54-
matrix[i][j] = 1 + min(
55-
matrix[i - 1][j - 1],
56-
matrix[i - 1][j], // add
57-
matrix[i][j - 1] // remove
58-
);
59-
}
48+
for(var j = 1; j <= word2.length; j++) {
49+
if(word1.charAt(i - 1) === word2.charAt(j - 1)) {
50+
matrix[i][j] = matrix[i - 1][j - 1];
51+
} 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+
);
6057
}
58+
}
6159
}
6260

6361
return matrix[word1.length][word2.length];
6462
};
6563

6664
var min = function(a, b, c) {
6765
if(a < b) {
68-
return (a < c) ? a : c;
66+
return (a < c) ? a : c;
6967
}
7068
return (b < c) ? b : c;
7169
}
@@ -76,29 +74,26 @@ var minDistance2 = function(word1, word2) {
7674
}
7775

7876
var minDistanceAux = function(word1, word2, iter1, iter2) {
79-
if(word1.length === iter1) {
80-
return word2.length - iter2;
81-
}
77+
if(word1.length === iter1)
78+
return word2.length - iter2;
8279

83-
if(word2.length === iter2) {
84-
return word1.length - iter1;
85-
}
80+
if(word2.length === iter2)
81+
return word1.length - iter1;
8682

87-
if(word1.charAt(iter1) === word2.charAt(iter2)) {
88-
return minDistanceAux(word1, word2, iter1 + 1, iter2 + 1);
89-
}
90-
91-
return 1 + min(
92-
minDistanceAux(word1, word2, iter1 + 1, iter2 + 1),
93-
minDistanceAux(word1, word2, iter1, iter2 + 1), // add
94-
minDistanceAux(word1, word2, iter1 + 1, iter2 ) // delete
95-
)
83+
if(word1.charAt(iter1) === word2.charAt(iter2))
84+
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+
)
9691
};
9792

9893
var min = function(a, b, c) {
99-
if(a < b) {
100-
return (a < c) ? a : c;
101-
}
94+
if(a < b)
95+
return (a < c) ? a : c;
96+
10297
return (b < c) ? b : c;
10398
}
10499

@@ -107,12 +102,11 @@ var main = function() {
107102
console.log("Approach 1");
108103
console.log(minDistance("ros", "horse"));
109104
console.log(minDistance("intention", "execution"));
105+
110106
console.log("-------------");
111107
console.log("Approach 2");
112108
console.log(minDistance2("ros", "horse"));
113109
console.log(minDistance2("intention", "execution"));
114110
}
115111

116-
main();
117-
118112
module.exports.main = main;

LeetcodeProblems/Escape_The_Ghosts.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ The number of ghosts will not exceed 100.
4343
* @return {boolean}
4444
*/
4545
var escapeGhosts = function(ghosts, target) {
46-
var minDistanceGhost = Number.POSITIVE_INFINITY;
46+
var distancePacman = getDistance([0,0], target);
4747
for(ghost in ghosts) {
48-
const distance = getDistance(ghosts[ghost], target);
49-
if(distance < minDistanceGhost) {
50-
minDistanceGhost = distance;
51-
}
48+
const distanceGhost = getDistance(ghosts[ghost], target);
49+
if(distancePacman > distanceGhost)
50+
return false
5251
}
53-
54-
var distancePacman = getDistance([0,0], target);
55-
return distancePacman < minDistanceGhost;
52+
53+
return true;
5654
};
5755

5856
var getDistance = function(a, b) {
@@ -67,4 +65,4 @@ var main = function() {
6765
console.log(escapeGhosts([[2, 0]], [1, 0]));
6866
}
6967

70-
module.exports.main = main
68+
module.exports.main = main
File renamed without changes.

0 commit comments

Comments
 (0)