Skip to content

Commit de7ba71

Browse files
committed
Continue dynamic programming 91, 152, 120 Tree 106 String 151
1 parent 7499c58 commit de7ba71

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed

Easy/104-maxDepth.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ var maxDepth = function(root) {
2222
}
2323
return max + 1;
2424
}
25+
26+
// second try
27+
var maxDepth = function(root) {
28+
if (!root) return 0;
29+
var lHeight = maxDepth(root.left) + 1;
30+
var rHeight = maxDepth(root.right) + 1;
31+
return lHeight > rHeight ? lHeight : rHeight;
32+
};

Medium/106-constructBinaryTree.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {number[]} inorder
10+
* @param {number[]} postorder
11+
* @return {TreeNode}
12+
*/
13+
var buildTree = function(inorder, postorder) {
14+
return helper(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
15+
};
16+
17+
var helper = function(inorder, inStart, inEnd, postorder, postStart, postEnd) {
18+
if (inEnd < inStart || postEnd < postStart) return null;
19+
var rootVal = postorder[postEnd];
20+
var root = new TreeNode(rootVal);
21+
for (var i = 0; i < inEnd; i++) {
22+
if (inorder[i] === rootVal) {
23+
break;
24+
}
25+
}
26+
root.left = helper(inorder, inStart , i - 1 , postorder, postStart, postStart + i - 1 - inStart);
27+
root.right = helper(inorder, i + 1, inEnd, postorder, postEnd - inEnd + i, postEnd - 1);
28+
return root;
29+
};

Medium/114-flattenTree.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,18 @@ var flatten = function(root) {
1313
while (oRoot.right) oRoot = oRoot.right;
1414
oRoot.right = oRight;
1515
};
16+
17+
// 2nd try
18+
var flatten = function(root) {
19+
if (!root) return;
20+
// save right now
21+
var tmp = root.right;
22+
flatten(root.left);
23+
flatten(root.right);
24+
root.right = root.left;
25+
root.left = null;
26+
while (root.right) {
27+
root = root.right;
28+
}
29+
root.right = tmp;
30+
};

Medium/120-triangle.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Dynamic programming
3+
*
4+
* @param {number[][]} triangle
5+
* @return {number}
6+
*/
7+
// straightforward, top-down, space O(N^2)
8+
var minimumTotal = function(triangle) {
9+
var dp = [triangle[0]];
10+
for (var i = 1; i < triangle.length; i++) {
11+
// in JavaScript, remember to have a new array for each row.
12+
dp[i] = [];
13+
for (var j = 0; j < triangle[i].length; j++) {
14+
if (j === 0) dp[i][j] = dp[i - 1][j] + triangle[i][j];
15+
else if (j === triangle[i].length - 1) dp[i][j] = dp[i - 1][j - 1] + triangle[i][j];
16+
else dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j]) + triangle[i][j];
17+
}
18+
}
19+
return Math.min.apply(null, dp[triangle.length - 1]);
20+
};
21+
22+
// bottom-up, space O(N)
23+
var minimumTotal = function(triangle) {
24+
var length = triangle.length;
25+
var sum = triangle[length - 1];
26+
for (var i = length - 2; i >= 0; i--) {
27+
for (var j = 0; j < triangle[i].length; j++) {
28+
sum[j] = Math.min(sum[j], sum[j + 1]) + triangle[i][j];
29+
}
30+
console.log(sum);
31+
}
32+
return sum[0];
33+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} str
3+
* @returns {string}
4+
*/
5+
var reverseWords = function(str) {
6+
var words = str.split(' ');
7+
// the O(1) space
8+
var j = 0;
9+
for (var i = 0; i < words.length; i++) {
10+
if (words[i] !== '') {
11+
words[j] = words[i];
12+
j++;
13+
}
14+
}
15+
return words.slice(0, j).reverse().join(' ');
16+
};

Medium/152-MaxProductSubarray.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Solution: Two dp arrays: maxDP tracks the Maximum product at index i,
3+
* minDP tracks the Minimum product at index i, either of them can be negative Numbers
4+
* so if nums[i + 1] is negative, then maxDP[i+1] can be a product of minDP[i] * nums[i+1]
5+
* @see the for loop body
6+
*
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var maxProduct = function(nums) {
11+
if (nums.length === 0) return 0;
12+
var result = nums[0];
13+
var maxDP = [nums[0]];
14+
var minDP = [nums[0]];
15+
16+
for (var i = 1; i < nums.length; i++) {
17+
maxDP[i] = Math.max(nums[i], maxDP[i - 1] * nums[i], minDP[i - 1] * nums[i]);
18+
minDP[i] = Math.min(nums[i], minDP[i - 1] * nums[i], maxDP[i - 1] * nums[i]);
19+
result = Math.max(maxDP[i], result);
20+
}
21+
22+
return result;
23+
};

Medium/91-decodeways.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* solution: dp[i] = dp[i - 1] if s[i - 1] is valid (!= 0)
3+
* or dp[i] = dp[i-2] if s[i-1] is not valid, however s.substring(i-2, i) is valid ( >=10 && <=26)
4+
* or dp[i] = dp[i-1] + dp[i-2] is both s.substring(i-2, i), s.substring(i-1, i) are valid
5+
*
6+
* @param {string} s
7+
* @return {number}
8+
*/
9+
var numDecodings = function(s) {
10+
if (s.length === 0) return 0;
11+
var dp = [1];
12+
s[0] === '0' ? dp[1] = 0 : dp[1] = 1;
13+
14+
for (var i = 2; i <= s.length; i++) {
15+
var prevTwo = parseInt(s.substring(i - 2, i));
16+
dp[i] = 0;
17+
if (s[i - 1] !== '0') dp[i] = dp[i - 1];
18+
if (prevTwo >= 10 && prevTwo <= 26 ) dp[i] += dp[i - 2];
19+
}
20+
21+
return dp[s.length];
22+
};

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,30 @@
112112
* [86. Partition List](https://leetcode.com/problems/partition-list/) - [Solution](./Medium/86-partition-List.js)
113113
* [89. Gray Code](https://leetcode.com/problems/gray-code/) - [Solution](./Medium/89-grayCode.js)
114114
* [90. Subsets II ](https://leetcode.com/problems/subsets-ii/) - [Solution](./Medium/90-subsetsII.js)
115+
* [91. Decode Ways ](https://leetcode.com/problems/decode-ways/) - [Solution](./Medium/91-decodeways.js)
115116
* [92. Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) - [Solution](./Medium/92-reverseLinkedListII.js)
116117
* [93. Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) - [Solution](./Medium/93-restoreIPAddresses.js)
117118
* [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) - [Solution](./Medium/94-binaryTreeInorder.js)
118119
* [96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) - [Solution](./Medium/96-uniqueBinarySearchTrees.js)
119120
* [98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) - [Solution](./Medium/98-validateBST.js)
120121
* [103. Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) - [Solution](./Medium/103-BSTZigzagLevelTraversal.js)
121122
* [105. Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) - [Solution](./Medium/105-constructBinaryTree.js)
123+
* [106. Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) - [Solution](./Medium/106-constructBinaryTree.js)
122124
* [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) - [Solution](./Medium/108-convertSortedArraytoBST.js)
123125
* [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) - [Solution](./Medium/109-convertSortedListToBST.js)
124126
* [113. Path Sum II](https://leetcode.com/problems/path-sum-ii/) - [Solution](./Medium/113-pathSumII.js)
125127
* [114. Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) - [Solution](./Medium/114-flattenTree.js)
126128
* [116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) - [Solution](./Medium/116-PopulatingNextRightPointersinEachNode.js)
129+
* [120. Triangle](https://leetcode.com/problems/triangle/) - [Solution](./Medium/120-triangle.js)
127130
* [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) - [Solution](./Medium/121-bestTimeToBuySellStock.js)
128131
* [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) - [Solution](./Medium/122-bestTimeToBuySellStockII.js)
129132
* [129. Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) - [Solution](./Medium/129-sumRootToLeafNumbers.js)
130133
* [134. Gas Station](https://leetcode.com/problems/gas-station/) - [Solution](./Medium/134-gasStation.js)
131134
* [136. Single Number](https://leetcode.com/problems/single-number/) - [Solution](./Medium/136-singleNumber.js)
132135
* [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) - [Solution](./Medium/141-linkedListCycle.js)
133136
* [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) - [Solution](./Medium/144-binaryTreePreorder.js)
137+
* [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) - [Solution](./Medium/151-reverseWordsInAString.js)
138+
* [152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) - [Solution](./Medium/152-MaxProductSubarray.js)
134139
* [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) - [Solution](./Medium/153-findMinimumInRotatedSortedArray.js)
135140
* [162. Find Peak Element](https://leetcode.com/problems/find-peak-element/) - [Solution](./Medium/162-findPeakElement.js)
136141
* [199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) - [Solution](./Medium/199-binaryTreeRightSideView.js)

0 commit comments

Comments
 (0)