Skip to content

Commit 93d9693

Browse files
committed
add some more
1 parent 16776e3 commit 93d9693

9 files changed

+354
-21
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// https://segmentfault.com/a/1190000003790746
2+
3+
/**
4+
* @param {string} s
5+
* @return {number}
6+
*/
7+
var lengthOfLongestSubstringTwoDistinct = function(s) {
8+
var longestSubstr = "";
9+
var maxLength = 0;
10+
var start = 0;
11+
var map = new Map();
12+
13+
for(var i = 0; i < s.length; i++) {
14+
var c = s.charAt(i);
15+
16+
// if map already contains two distrinct chars and the char is new to the map
17+
if(map.size > 1 && map.get(c) === undefined) {
18+
var leftMost = s.length;
19+
20+
// Calc substring len before the new char
21+
if(i - start > maxLength) {
22+
// Should not include i, since i is the new distinct char's index
23+
longestSubstr = s.substring(start, i);
24+
maxLength = longestSubstr.length;
25+
}
26+
27+
map.forEach((charIdx, key)=> {
28+
if(charIdx < leftMost) {
29+
leftMost = charIdx;
30+
}
31+
});
32+
33+
start = leftMost + 1;
34+
map.delete(s[leftMost]);
35+
}
36+
37+
map.set(c, i);
38+
}
39+
40+
if(s.length - start > maxLength) {
41+
longestSubstr = s.substring(start, s.length);
42+
maxLength = longestSubstr.length;
43+
}
44+
45+
return maxLength;
46+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 {TreeNode} root
10+
* @return {number[][]}
11+
*/
12+
13+
var verticalOrder = function(root) {
14+
var map = {};
15+
var queue = [];
16+
var res = [];
17+
// since iterating over map cannot ensure order
18+
// we store min and max index for enforcing that
19+
var minIdx = Infinity;
20+
var maxIdx = -Infinity;
21+
22+
if(root === null) {
23+
return res;
24+
}
25+
26+
queue.push([0, root]);
27+
while(queue.length > 0) {
28+
var len = queue.length;
29+
30+
for(var i = 0; i < len; i++) {
31+
var data = queue.shift();
32+
var cur = data[1];
33+
var idx = data[0];
34+
35+
if(idx < minIdx) {
36+
minIdx = idx;
37+
}
38+
if(idx > maxIdx) {
39+
maxIdx = idx;
40+
}
41+
42+
map[idx] = map[idx] || [];
43+
map[idx].push(cur.val);
44+
45+
if(cur.left) {
46+
queue.push([idx - 1, cur.left]);
47+
}
48+
if(cur.right) {
49+
queue.push([idx + 1, cur.right]);
50+
}
51+
}
52+
}
53+
// since iterating over map cannot ensure order
54+
for(i = minIdx; i <= maxIdx; i++) {
55+
var key = i.toString();
56+
57+
if(map[key]) {
58+
res.push(map[key]);
59+
}
60+
}
61+
62+
return res;
63+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var lengthOfLongestSubstringKDistinct = function(s, k) {
7+
var longestSubstr = "";
8+
var maxLength = 0;
9+
var start = 0;
10+
var map = new Map();
11+
12+
if(k === 0) {
13+
return 0;
14+
}
15+
16+
for(var i = 0; i < s.length; i++) {
17+
var c = s.charAt(i);
18+
19+
// if map already contains two distrinct chars and the char is new to the map
20+
if(map.size >= k && map.get(c) === undefined) {
21+
var leftMost = s.length;
22+
23+
// Calc substring len before the new char
24+
if(i - start > maxLength) {
25+
// Should not include i, since i is the new distinct char's index
26+
longestSubstr = s.substring(start, i);
27+
maxLength = longestSubstr.length;
28+
}
29+
30+
map.forEach((charIdx, key)=> {
31+
if(charIdx < leftMost) {
32+
leftMost = charIdx;
33+
}
34+
});
35+
36+
start = leftMost + 1;
37+
map.delete(s[leftMost]);
38+
}
39+
40+
map.set(c, i);
41+
}
42+
43+
if(s.length - start > maxLength) {
44+
longestSubstr = s.substring(start, s.length);
45+
maxLength = longestSubstr.length;
46+
}
47+
48+
return maxLength;
49+
};

56. Merge Intervals.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for an interval.
3+
* function Interval(start, end) {
4+
* this.start = start;
5+
* this.end = end;
6+
* }
7+
*/
8+
/**
9+
* @param {Interval[]} intervals
10+
* @return {Interval[]}
11+
*/
12+
var merge = function(intervals) {
13+
14+
// http://yucoding.blogspot.com/2013/01/leetcode-question-51-merge-intervals.html
15+
16+
var result = [];
17+
18+
if(!intervals || intervals.length === 0) {
19+
return result;
20+
}
21+
22+
intervals.sort((a, b)=> {
23+
return a.start > b.start ? 1 : -1;
24+
});
25+
26+
result.push(intervals[0]);
27+
28+
for(var i = 1; i < intervals.length; i++) {
29+
var topOfResult = result[result.length - 1];
30+
var interval = intervals[i];
31+
32+
if(topOfResult.end >= interval.start) {
33+
topOfResult.end = Math.max(topOfResult.end, interval.end);
34+
} else {
35+
result.push(interval);
36+
}
37+
}
38+
39+
return result;
40+
};

57. Insert Interval.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for an interval.
3+
* function Interval(start, end) {
4+
* this.start = start;
5+
* this.end = end;
6+
* }
7+
*/
8+
/**
9+
* @param {Interval[]} intervals
10+
* @param {Interval} newInterval
11+
* @return {Interval[]}
12+
*/
13+
14+
// http://bangbingsyb.blogspot.com/2014/11/leetcode-insert-interval.html
15+
var insert = function(intervals, newInterval) {
16+
var result = [];
17+
// Easier to consider if two sections are not overlapped
18+
// [s1, e1] [s2, e2] --> e2 < s1 or e1 < s2
19+
// once merged when two sections are overlapped, [min(s1,s2), max(e1,e2)]
20+
21+
var isInsert = false;
22+
23+
for(var i = 0; i < intervals.length; i++) {
24+
var interval = intervals[i];
25+
26+
if(isInsert) {
27+
result.push(interval);
28+
} else if(newInterval.end < interval.start) {
29+
result.push(newInterval);
30+
result.push(interval);
31+
isInsert = true;
32+
} else if(newInterval.start <= interval.end && interval.start <= newInterval.end){
33+
34+
newInterval.start = Math.min(newInterval.start, interval.start);
35+
newInterval.end = Math.max(newInterval.end, interval.end);
36+
continue;
37+
} else {
38+
result.push(interval);
39+
}
40+
}
41+
42+
if(!isInsert) {
43+
result.push(newInterval);
44+
}
45+
46+
return result;
47+
};

96 Unique Binary Search Trees.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @param {number} n
33
* @return {number}
44
*/
5+
// https://www.youtube.com/watch?v=YDf982Lb84o
56
var numTrees = function(n) {
67
var result = [1, 1];
78

@@ -14,3 +15,35 @@ var numTrees = function(n) {
1415
}
1516
return result[n];
1617
};
18+
19+
20+
// var numTrees = function(n) {
21+
// var i,
22+
// j,
23+
// result = [];
24+
25+
// result[0] = 1;
26+
// result[1] = 1;
27+
28+
// for (i = 2; i <= n; i++) {
29+
// result[i] = 0;
30+
// for (j = 0; j < i; j++) {
31+
// result[i] += result[j] * result[i - 1 - j];
32+
// console.log(i, j, i - 1 - j, result)
33+
// }
34+
35+
// }
36+
// return result[n];
37+
// };
38+
39+
40+
numTrees(4);
41+
42+
43+
1, 2, 3, 4
44+
45+
j = r[0]
46+
k = r[3]
47+
48+
j = r[1]
49+
k = r[2]

Javascript Closure Questions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// implement function add in such a way that:
2+
// add() => 0
3+
// add(1)(2)() => 3
4+
// add(n0)(n1)...(nk)() => n0+n1+...+nk
5+
6+
function add(val){
7+
var total = 0;
8+
var result;
9+
var step = function(val){
10+
if(val === undefined){
11+
result = total;
12+
total = 0;
13+
return result;
14+
} else {
15+
total += val;
16+
}
17+
18+
return step;
19+
}
20+
21+
result = step(val);
22+
23+
return result;
24+
}

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Leetcode Problems in Javascript
4242
152 Maximum Product Subarray.js
4343
153 Find Minimum in Rotated Sorted Array.js
4444
155 Min Stack.js
45+
159 Longest Substring with At Most Two Disctinct Characters.js
4546
16 3Sum Closest.js
4647
160 Intersection Of Two Linked Lists.js
4748
162 Find Peak Element.js
@@ -75,7 +76,6 @@ Leetcode Problems in Javascript
7576
21 Merge Two Sorted Lists.js
7677
211 Add and Search Word - Data structure design.js
7778
215 Kth Largest Element in an Array.js
78-
215 Kth Largest Element in an Array.py
7979
217 Contain Duplicate.js
8080
219 Contains Duplicate II.js
8181
22 Generate Parentheses.js
@@ -111,11 +111,13 @@ Leetcode Problems in Javascript
111111
3 Longest Substring Without Repeating Characters.js
112112
300 Longest Increasing Subsequence.js
113113
31 Next Permutation.js
114+
314 Binary Tree Vertical Order Traversal.js
114115
318 Maximum Product of Word Lengths My Submissions Question.js
115116
322 Coin Change.js
116117
33 Search in Rotated Sorted Array.js
117118
335 Self Crossing.js
118119
34 Search for a Range.js
120+
340 Longest Substring With At Most K Distinct Characters.js
119121
35 Search Insert Position.js
120122
36 Valid Sudoku.js
121123
37 Sudoku Solver.js
@@ -134,6 +136,8 @@ Leetcode Problems in Javascript
134136
53 Maximum Subarray.js
135137
54 Spiral Matrix.js
136138
55 Jump Game.js
139+
56. Merge Intervals.js
140+
57. Insert Interval.js
137141
58 Length of Last Word.js
138142
59 Spiral Matrix II.js
139143
61 Rotate List.js
@@ -163,4 +167,12 @@ Leetcode Problems in Javascript
163167
93 Restore IP Addresses.js
164168
94 Binary Tree Inorder Traversal.js
165169
96 Unique Binary Search Trees.js
166-
98 Validate Binary Search Tree.js
170+
98 Validate Binary Search Tree.js
171+
Hackrank.js
172+
Javascript Closure Questions.js
173+
Jump Game II.js
174+
Rate Limiter.js
175+
Reverse Integer.js
176+
String Encoding and Decoding.js
177+
find kth element in two arrays.js
178+
flatten nested array.js

0 commit comments

Comments
 (0)