Skip to content

Commit 40e026a

Browse files
author
Chihung Yu
committed
add comment
1 parent aa9af43 commit 40e026a

File tree

5 files changed

+157
-10
lines changed

5 files changed

+157
-10
lines changed

11 Container With Most Water.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
// Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
2+
//
3+
// Note: You may not slant the container and n is at least 2.
4+
//
5+
// The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
6+
//
7+
// Example:
8+
//
9+
// Input: [1,8,6,2,5,4,8,3,7]
10+
// Output: 49
11+
//
12+
13+
14+
// Given fixed set of vertical bars
15+
// Min case
16+
//
17+
// . .
18+
// . . . .
19+
// . . . . . .
20+
21+
// Max case
22+
//
23+
// . .
24+
// . . . .
25+
// . . . . . .
26+
127
/**
228
* @param {number[]} height
329
* @return {number}
@@ -6,17 +32,17 @@ var maxArea = function(height) {
632
var left = 0;
733
var right = height.length - 1;
834
var maxVal = 0;
9-
35+
1036
while(left<right){
1137
var contain = (right-left)*Math.min(height[left],height[right]);
1238
maxVal = Math.max(contain, maxVal);
13-
39+
1440
if(height[left] >= height[right]){
1541
right--;
1642
} else {
1743
left++;
1844
}
1945
}
20-
46+
2147
return maxVal;
22-
};
48+
};

31 Next Permutation.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
var nextPermutation = function(nums) {
77
var vioIndex = nums.length - 1;
88

9+
10+
// example
11+
// 1. 687432
12+
// the violation index is 1 (number 8) since it's greater than index 0 (number 6)
13+
// 2. 87452
14+
// the violation index is 3 (number 5) since it's greater than index 2 (number 4)
915
while(vioIndex > 0) {
1016
if(nums[vioIndex - 1] < nums[vioIndex]) {
1117
break;
1218
}
1319
vioIndex--;
1420
}
15-
21+
22+
23+
// If it's not already the maximum value i.e. 876432 in the example of 687432 the maximum is 876432
24+
// then swap the the 6 with 7 since 7 a just a tad bigger
1625
if(vioIndex > 0) {
1726
vioIndex--;
1827
var first = nums.length - 1;
@@ -37,4 +46,49 @@ var nextPermutation = function(nums) {
3746
end--;
3847
vioIndex++;
3948
}
49+
};
50+
51+
52+
53+
/**
54+
* @param {number[]} nums
55+
* @return {void} Do not return anything, modify nums in-place instead.
56+
*/
57+
var nextPermutation = function(nums) {
58+
var violatedIndex = nums.length - 1;
59+
60+
while(violatedIndex > 0) {
61+
if (nums[violatedIndex] > nums[violatedIndex-1]) {
62+
break;
63+
}
64+
violatedIndex--;
65+
}
66+
67+
// max
68+
if (violatedIndex > 0) {
69+
violatedIndex--;
70+
71+
var indexToSwapWith = nums.length - 1;
72+
while(indexToSwapWith > violatedIndex) {
73+
if (nums[indexToSwapWith] > nums[violatedIndex]) {
74+
var temp = nums[violatedIndex];
75+
nums[violatedIndex] = nums[indexToSwapWith];
76+
nums[indexToSwapWith] = temp;
77+
break;
78+
}
79+
indexToSwapWith--
80+
}
81+
82+
violatedIndex++;
83+
}
84+
85+
var end = nums.length - 1;
86+
87+
while(end > violatedIndex) {
88+
temp = nums[violatedIndex];
89+
nums[violatedIndex] = nums[end];
90+
nums[end] = temp;
91+
end--
92+
violatedIndex++;
93+
}
4094
};

55 Jump Game.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ var canJump = function(nums) {
1111
}
1212
numLeft = Math.max(nums[i], numLeft);
1313
}
14-
15-
return numLeft >= 0;
14+
return true;
1615
};

76 Minimum Window Substring.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ var minWindow = function(s, t) {
2626
var lenS = s.length;
2727
var lenT = t.length;
2828
var queue = [];
29-
var tRequireCount = {};
30-
var tFoundCount = {};
29+
var tRequireCount = {}; // string T required count
30+
var tFoundCount = {}; // string T found count
3131
var hasFound = 0;
3232
var windowBeg = -1;
3333
var windowEnd = lenS;
@@ -41,6 +41,7 @@ var minWindow = function(s, t) {
4141
}
4242

4343
for(i = 0; i < lenS; i++) {
44+
// cannot use tRequireCount[s[i]] !== 0 since tRequireCount[s[i]] might be undefined
4445
if(tRequireCount[s[i]] > 0) {
4546
// use queue to skip a lot of redudant character
4647
// minWindow('aeeeeeebceeeeaeeedcb', 'abc');
@@ -53,7 +54,13 @@ var minWindow = function(s, t) {
5354

5455
// if found count is over require count, we don't need those extra, so don't record it to hasFound
5556
if(tFoundCount[s[i]] <= tRequireCount[s[i]]) {
56-
hasFound++;
57+
// hasFound is used as a flag for knowing where we should check against T string and when we should start reducing the range
58+
// for example
59+
// minWindow('aaaaebceeeeaeeedcb', 'abc');
60+
// at index 5 where s[i] is e, hasFound is 1 and tFoundCount['a'] is 4, queue=[0,1,2,3]
61+
// when we arrive at index 7 where s[i] is c, has found is now 3 and is equal to lenT
62+
// now we can start reducing the range all the way from index 0(a) to index 7(c) to index 3(a) to index 7 (c) which will give us the min
63+
hasFound++;
5764
}
5865

5966
// when the current location which is in queue
@@ -73,9 +80,67 @@ var minWindow = function(s, t) {
7380
// 1st round 0 8
7481
// 2nd round 7 13
7582
}
83+
// since i must be the last match that leads to hasFound === lenT
84+
hasFound--;
85+
}
86+
}
87+
}
88+
89+
return windowBeg !== -1 ? s.substring(windowBeg, windowEnd + 1) : '';
90+
};
91+
7692

93+
94+
/**
95+
* @param {string} s
96+
* @param {string} t
97+
* @return {string}
98+
*/
99+
var minWindow = function(s, t) {
100+
var lenS = s.length;
101+
var lenT = t.length;
102+
var tCharToFoundCount = {};
103+
var tCharToRequiredCount = {};
104+
105+
for(var i = 0; i < lenT; i++) {
106+
var c = t[i];
107+
tCharToFoundCount[c] = 0;
108+
tCharToRequiredCount[c] = tCharToRequiredCount[c] || 0;
109+
tCharToRequiredCount[c]++;
110+
}
111+
112+
var windowBeg = -1;
113+
var windowEnd = lenS;
114+
var queue = [];
115+
var hasFound = 0;
116+
for(i = 0; i < lenS; i++) {
117+
c = s[i];
118+
// skip unneeded char
119+
if (tCharToRequiredCount[c] !== 0) {
120+
tCharToFoundCount[c]++;
121+
queue.push(i);
122+
123+
if (tCharToFoundCount[c] <= tCharToRequiredCount[c]) {
124+
hasFound++;
125+
}
126+
127+
if (hasFound === lenT) {
128+
var k;
129+
130+
do {
131+
k = queue.shift();
132+
tCharToFoundCount[s[k]]--;
133+
} while(tCharToFoundCount[s[k]] >= tCharToRequiredCount[s[k]]);
134+
135+
if (windowEnd - windowBeg > i - k) {
136+
windowBeg = k;
137+
windowEnd = i;
138+
}
139+
77140
hasFound--;
78141
}
142+
143+
79144
}
80145
}
81146

[0003] Longest Substring Without Repeating Characters.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55

66

7+
// use map for storing index
8+
// if a repeated character is found, skip directly to the index of the repeated character in the map.
9+
710
var lengthOfLongestSubstring = function(s) {
811
if(s === null || s.length === 0){
912
return 0;

0 commit comments

Comments
 (0)