Skip to content

Commit 05f0d50

Browse files
authored
Merge pull request DaleStudy#111 from yolophg/main
[Helena] Week 6 solutions
2 parents f3d4e01 + 9ddfdfb commit 05f0d50

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

container-with-most-water/yolophg.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var maxArea = function(height) {
5+
let left = 0;
6+
let right = height.length - 1;
7+
8+
// to store the maximum area.
9+
let maxArea = 0;
10+
11+
// iterate until the two pointers meet.
12+
while (left < right) {
13+
// calculate the width between the two pointers.
14+
let width = right - left;
15+
16+
// calculate the area with the current area.
17+
let currentArea = Math.min(height[left], height[right]) * width;
18+
19+
// update the maximum area if the current area is larger.
20+
maxArea = Math.max(maxArea, currentArea);
21+
22+
// move the pointer to the shorter line towards the center.
23+
if (height[left] < height[right]) {
24+
left++;
25+
} else {
26+
right--;
27+
}
28+
}
29+
30+
// return the maximum area found.
31+
return maxArea;
32+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(log n)
2+
// Space Complexity: O(1)
3+
4+
var findMin = function(nums) {
5+
// initialize left pointer.
6+
let left = 0;
7+
// initialize right pointer.
8+
let right = nums.length - 1;
9+
10+
while (left < right) {
11+
// calculate the middle index.
12+
let mid = left + ((right - left) / 2 | 0);
13+
14+
// if the value at the middle index is bigger, move the left pointer to the right.
15+
if (nums[mid] > nums[right]) {
16+
left = mid + 1;
17+
} else { // else, move the right pointer to the middle index.
18+
right = mid;
19+
}
20+
}
21+
22+
// return the element which is the minimum pointed to by the left pointer.
23+
return nums[left];
24+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var characterReplacement = function(s, k) {
5+
let maxLen = 0;
6+
// initialize an array to store the count of each character.
7+
let charCounts = new Array(26).fill(0);
8+
let start = 0;
9+
10+
// iterate the string with the end pointer.
11+
for (let end = 0; end < s.length; end++) {
12+
// calculate the index of the character.
13+
let charIndex = s.charCodeAt(end) - 65;
14+
// update maxCount with the maximum count.
15+
maxLen = Math.max(maxLen, ++charCounts[charIndex]);
16+
17+
// move the start pointer and decrement the count at that position.
18+
if (end - start + 1 - maxLen > k) {
19+
charCounts[s.charCodeAt(start) - 65]--;
20+
start++;
21+
}
22+
}
23+
24+
return s.length - start;
25+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var lengthOfLongestSubstring = function(s) {
5+
let charSet = new Set();
6+
7+
let left = 0;
8+
let right = 0;
9+
10+
let maxLength = 0;
11+
12+
// iterate the string.
13+
while (right < s.length) {
14+
if (!charSet.has(s[right])) {
15+
// if the character isn't in the set, add it.
16+
charSet.add(s[right]);
17+
// move the right pointer to the right.
18+
right++;
19+
// update the maximum length if the current window is larger.
20+
maxLength = Math.max(maxLength, right - left);
21+
} else {
22+
// if the character is in the set, remove the leftmost character.
23+
charSet.delete(s[left]);
24+
// move the left pointer to the right.
25+
left++;
26+
}
27+
}
28+
29+
return maxLength;
30+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var search = function(nums, target) {
5+
// iterate through each element.
6+
for (let i = 0; i < nums.length; i++) {
7+
// check if the current element is equal to the target.
8+
if (nums[i] === target) {
9+
// if found, return the index.
10+
return i;
11+
}
12+
}
13+
14+
// if the loop completes without finding the target, return -1.
15+
return -1;
16+
};

0 commit comments

Comments
 (0)