Skip to content

Commit e3c86bc

Browse files
committed
add more solutions
1 parent aa76f90 commit e3c86bc

19 files changed

+787
-73
lines changed

124 Binary Tree Maximum Path Sum.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Given a binary tree, find the maximum path sum.
2+
3+
// For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
4+
5+
// For example:
6+
// Given the below binary tree,
7+
8+
// 1
9+
// / \
10+
// 2 3
11+
// Return 6.
12+
13+
114
// http://bangbingsyb.blogspot.com/2014/11/leetcode-binary-tree-maximum-path-sum.html
215
/**
316
* Definition for a binary tree node.
@@ -22,13 +35,17 @@ var maxPathSum = function(root) {
2235

2336
var leftVal = Math.max(findMaxPath(node.left), 0);
2437
var rightVal = Math.max(findMaxPath(node.right), 0);
25-
38+
2639
var ps1 = node.val + Math.max(leftVal, rightVal);
2740
// ps2 means taking this current node as parent node and stop there
2841
var ps2 = node.val + leftVal + rightVal;
2942

43+
// maxVal as if we end counting value here, what will be the maximum val
44+
// leftVal and rightVal can be negative values
3045
maxVal = Math.max(maxVal, Math.max(ps1, ps2));
46+
3147
// return ps1 only since, ps2 cannot be combined with the parent node
48+
// leftVal and rightVal can be negative values, however, we can to see if combining with values down below can give higher number
3249
return ps1;
3350
}
3451
};

224 Basic Calculator.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
Implement a basic calculator to evaluate a simple expression string.
3+
4+
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
5+
6+
You may assume that the given expression is always valid.
7+
8+
Some examples:
9+
"1 + 1" = 2
10+
" 2-1 + 2 " = 3
11+
"(1+(4+5+2)-3)+(6+8)" = 23
12+
Note: Do not use the eval built-in library function.
13+
*/
14+
/**
15+
* @param {string} s
16+
* @return {number}
17+
*/
18+
var calculate = function(s) {
19+
var stack = [],
20+
len = s.length,
21+
sum = 0,
22+
num,
23+
ch,
24+
j,
25+
i;
26+
27+
stack.push(1);
28+
stack.push(1);
29+
30+
for (i = 0; i < len; i++) {
31+
ch = s.charAt(i);
32+
33+
if (!isNaN(parseInt(ch))) {
34+
num = parseInt(ch);
35+
36+
for (j = i + 1; j < len && !isNaN(parseInt(s.charAt(j))); j++) {
37+
num = num * 10 + parseInt(s.charAt(j));
38+
}
39+
40+
sum += stack.pop() * num;
41+
42+
i = j - 1;
43+
} else if (ch === '+' || ch === '(') {
44+
stack.push(stack[stack.length - 1]);
45+
} else if (ch === '-') {
46+
stack.push(stack[stack.length - 1] * (-1));
47+
} else if (ch === ')') {
48+
stack.pop();
49+
}
50+
}
51+
52+
return sum;
53+
};

23 Merge k Sorted Lists.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
2+
// http://www.cnblogs.com/springfor/p/3869217.html
3+
/**
4+
* Definition for singly-linked list.
5+
* function ListNode(val) {
6+
* this.val = val;
7+
* this.next = null;
8+
* }
9+
*/
10+
/**
11+
* @param {ListNode[]} lists
12+
* @return {ListNode}
13+
*/
14+
var mergeKLists = function(lists) {
15+
return mergeHelper(lists, 0, lists.length - 1);
16+
};
17+
18+
function mergeHelper(lists, l, r) {
19+
if(l === r) {
20+
return lists[l];
21+
}
22+
23+
if(l > r) {
24+
return null;
25+
}
26+
27+
var mid = Math.floor((l + r)/2);
28+
var left = mergeHelper(lists, l, mid);
29+
var right = mergeHelper(lists, mid + 1, r);
30+
31+
return mergeTwoLists(left, right);
32+
}
33+
34+
35+
function mergeTwoLists(l1, l2) {
36+
var dummy = new ListNode(0);
37+
var cur = dummy;
38+
39+
while(l1 && l2) {
40+
if(l1.val < l2.val) {
41+
cur.next = l1;
42+
l1 = l1.next;
43+
} else {
44+
cur.next = l2;
45+
l2 = l2.next;
46+
}
47+
48+
cur = cur.next;
49+
}
50+
51+
if(l1) {
52+
cur.next = l1;
53+
}
54+
55+
if(l2) {
56+
cur.next = l2;
57+
}
58+
59+
return dummy.next;
60+
}

239 Sliding Window Maximum.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
2+
3+
// For example,
4+
// Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.
5+
6+
// Window position Max
7+
// --------------- -----
8+
// [1 3 -1] -3 5 3 6 7 3
9+
// 1 [3 -1 -3] 5 3 6 7 3
10+
// 1 3 [-1 -3 5] 3 6 7 5
11+
// 1 3 -1 [-3 5 3] 6 7 5
12+
// 1 3 -1 -3 [5 3 6] 7 6
13+
// 1 3 -1 -3 5 [3 6 7] 7
14+
// Therefore, return the max sliding window as [3,3,5,5,6,7].
15+
16+
/**
17+
* @param {number[]} nums
18+
* @param {number} k
19+
* @return {number[]}
20+
*/
21+
var maxSlidingWindow = function(nums, k) {
22+
var result = [],
23+
linkedListWithTwoEndsOps = [],
24+
len = nums.length,
25+
i;
26+
27+
if (k > len || k === 0) {
28+
return result;
29+
}
30+
31+
for (i = 0; i < len; i++) {
32+
// Remove anything that is less than the current value
33+
// so linkedListWithTwoEndsOps maintains values greater than the curret value
34+
while (linkedListWithTwoEndsOps.length > 0 && nums[linkedListWithTwoEndsOps[linkedListWithTwoEndsOps.length - 1]] < nums[i]) {
35+
var val = linkedListWithTwoEndsOps.pop();
36+
}
37+
38+
// In case that all elements in the linkedListWithTwoEndsOps are all greater than the current one (descending order)
39+
// Shift out the
40+
if (linkedListWithTwoEndsOps[0] < i - k + 1) {
41+
linkedListWithTwoEndsOps.shift();
42+
}
43+
44+
linkedListWithTwoEndsOps.push(i);
45+
46+
// For each sliding window movement, we record the highest value in that sliding window
47+
// i >= k - 1 to ensure that we don't prematurely record values before we get to the full range of the first sliding window
48+
// e.g. [1 3 -1] -3 5 3 6 7 3
49+
// this ensure that i is at least at -1 (index 2)
50+
if (i >= k - 1) {
51+
result.push(nums[linkedListWithTwoEndsOps[0]]);
52+
}
53+
}
54+
55+
return result;
56+
};

242 Valid Anagram.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
// Leetcode #242
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/valid-anagram/
4-
// Author: Chihung Yu
1+
// Given two strings s and t, write a function to determine if t is an anagram of s.
2+
3+
// For example,
4+
// s = "anagram", t = "nagaram", return true.
5+
// s = "rat", t = "car", return false.
6+
7+
8+
59
/**
610
* @param {string} s
711
* @param {string} t
812
* @return {boolean}
913
*/
1014
var isAnagram = function(s, t) {
11-
if((s === null || t === null) || (s.length !== t.length)){
12-
return false;
13-
}
14-
15-
var hash = {};
16-
17-
for(var i = 0; i < s.length; i++){
18-
hash[s[i]] = hash[s[i]] || 0;
19-
hash[s[i]]++;
20-
}
21-
22-
for(var j = 0; j < t.length; j++){
23-
if(!hash[t[j]]){
24-
return false;
25-
}
26-
27-
hash[t[j]]--;
28-
}
29-
30-
return true;
15+
var slen = s.length;
16+
var tlen = t.length;
17+
18+
if(slen !== tlen) {
19+
return false;
20+
}
21+
22+
var hash = {};
23+
24+
for(var i = 0; i < slen; i++) {
25+
var char = s[i];
26+
hash[char] = hash[char] || 0;
27+
hash[char]++;
28+
}
29+
30+
for(i = 0; i < tlen; i++) {
31+
char = t[i];
32+
33+
if(hash[char] === undefined || hash[char] === 0) {
34+
return false;
35+
}
36+
37+
hash[char]--;
38+
}
39+
40+
return true;
3141
};

25 Reverse Nodes in k-Group.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
3+
4+
// If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
5+
6+
// You may not alter the values in the nodes, only nodes itself may be changed.
7+
8+
// Only constant memory is allowed.
9+
10+
// For example,
11+
// Given this linked list: 1->2->3->4->5
12+
13+
// For k = 2, you should return: 2->1->4->3->5
14+
15+
// For k = 3, you should return: 3->2->1->4->5
16+
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+
* @param {number} k
28+
* @return {ListNode}
29+
*/
30+
31+
// http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/
32+
var reverseKGroup = function(head, k) {
33+
var cur = head;
34+
var pre = null;
35+
var post = null;
36+
var count = 0;
37+
38+
while(cur !== null && count < k) {
39+
cur = cur.next;
40+
count++;
41+
}
42+
43+
if(count !== k) {
44+
return head;
45+
}
46+
47+
cur = head;
48+
49+
while(cur !== null && count > 0) {
50+
post = cur.next;
51+
cur.next = pre;
52+
pre = cur;
53+
cur = post;
54+
count--;
55+
}
56+
57+
// post is now a pointer to (k+1)th node
58+
// recursively call for the list starting from cur
59+
if(post !== null) {
60+
head.next = reverseKGroup(post, k);
61+
}
62+
63+
return pre;
64+
};

0 commit comments

Comments
 (0)