Skip to content

Commit 5379515

Browse files
authored
Merge pull request DaleStudy#116 from sounmind/main
Evan: Week 6
2 parents 130c3df + ecb222f commit 5379515

File tree

5 files changed

+168
-0
lines changed
  • container-with-most-water
  • find-minimum-in-rotated-sorted-array
  • longest-repeating-character-replacement
  • longest-substring-without-repeating-characters
  • search-in-rotated-sorted-array

5 files changed

+168
-0
lines changed

container-with-most-water/evan.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} heights
3+
* @return {number}
4+
*/
5+
var maxArea = function (heights) {
6+
let maxAmount = 0;
7+
let [leftEnd, rightEnd] = [0, heights.length - 1];
8+
9+
while (leftEnd < rightEnd) {
10+
const minHeight = Math.min(heights[leftEnd], heights[rightEnd]);
11+
const horizontalLength = rightEnd - leftEnd;
12+
13+
maxAmount = Math.max(maxAmount, minHeight * horizontalLength);
14+
15+
if (heights[leftEnd] > heights[rightEnd]) {
16+
rightEnd -= 1;
17+
} else {
18+
leftEnd += 1;
19+
}
20+
}
21+
22+
return maxAmount;
23+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function (nums) {
6+
let [left, right] = [0, nums.length - 1];
7+
8+
while (left < right) {
9+
const mid = Math.floor((left + right) / 2);
10+
11+
if (nums[mid] > nums[right]) {
12+
// the smallest element is in the right half
13+
left = mid + 1;
14+
} else {
15+
// the smallest element is in the left half or at mid
16+
right = mid;
17+
}
18+
}
19+
20+
return nums[left];
21+
};
22+
23+
/**
24+
* The time complexity of this approach is O(log n), where n is the number of elements in the array.
25+
* This is because we are effectively halving the search space in each step of the binary search.
26+
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {string} str
3+
* @param {number} maxConvertCount
4+
* @return {number}
5+
*/
6+
var characterReplacement = function (str, maxConvertCount) {
7+
const freqInWindow = {};
8+
let maxLength = 0;
9+
let maxCharFreqInWindow = 0;
10+
let leftIndex = 0;
11+
12+
for (let rightIndex = 0; rightIndex < str.length; rightIndex++) {
13+
const slidingWindow = [str[leftIndex], str[rightIndex]];
14+
const [leftChar, rightChar] = slidingWindow;
15+
16+
freqInWindow[rightChar] = (freqInWindow[rightChar] || 0) + 1;
17+
maxCharFreqInWindow = Math.max(
18+
maxCharFreqInWindow,
19+
freqInWindow[rightChar]
20+
);
21+
22+
// while (currentWindowLength > maxConvertCount + maxCharFreqInWindow) {
23+
// while (cannot convert all characters in the window to the same character) {
24+
while (rightIndex - leftIndex + 1 > maxConvertCount + maxCharFreqInWindow) {
25+
leftIndex += 1;
26+
freqInWindow[leftChar] -= 1;
27+
}
28+
29+
maxLength = Math.max(maxLength, rightIndex - leftIndex + 1);
30+
}
31+
32+
return maxLength;
33+
};
34+
35+
/**
36+
* Time Complexity: O(n)
37+
* - The right pointer iterates through the string once, making it O(n).
38+
* - The left pointer only moves when necessary, also contributing to O(n) overall.
39+
* - Thus, the total time complexity is O(n).
40+
*
41+
* Space Complexity: O(1)
42+
* - The frequency object stores counts for at most 26 letters in the English alphabet.
43+
* - Since the space used by the frequency object is constant and does not scale with input size,
44+
* the space complexity is O(1).
45+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} str
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function (str) {
6+
let maxLength = 0;
7+
let left = 0;
8+
const lastSeenCharIndexMap = {};
9+
10+
for (let right = 0; right < str.length; right++) {
11+
const currentChar = str[right];
12+
13+
// If the current character was seen before, update the left pointer
14+
if (lastSeenCharIndexMap[currentChar] >= left) {
15+
left = lastSeenCharIndexMap[currentChar] + 1;
16+
}
17+
18+
lastSeenCharIndexMap[currentChar] = right;
19+
20+
maxLength = Math.max(maxLength, right - left + 1);
21+
}
22+
23+
return maxLength;
24+
};
25+
26+
/**
27+
* Time Complexity: O(n)
28+
* - The right pointer iterates through the string once, making it O(n).
29+
* - The left pointer only moves when a duplicate character is found, ensuring each character is processed at most twice.
30+
* - Thus, the total time complexity is O(n).
31+
*
32+
* Space Complexity: O(min(n, m))
33+
* - The space complexity is determined by the size of the character map, which stores the indices of characters in the current window.
34+
* - In the worst case, the map could store all unique characters in the string.
35+
* - Thus, the space complexity is O(min(n, m)), where n is the length of the string and m is the character set size.
36+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var search = function (nums, target) {
7+
let leftIndex = 0;
8+
let rightIndex = nums.length - 1;
9+
10+
while (leftIndex <= rightIndex) {
11+
const midIndex = Math.floor((rightIndex + leftIndex) / 2);
12+
const [leftValue, midValue, rightValue] = [
13+
nums[leftIndex],
14+
nums[midIndex],
15+
nums[rightIndex],
16+
];
17+
18+
if (midValue === target) {
19+
return midIndex;
20+
}
21+
22+
if (leftValue <= midValue) {
23+
if (leftValue <= target && target < midValue) {
24+
rightIndex = midIndex - 1;
25+
} else {
26+
leftIndex = midIndex + 1;
27+
}
28+
} else {
29+
if (midValue < target && target <= rightValue) {
30+
leftIndex = midIndex + 1;
31+
} else {
32+
rightIndex = midIndex - 1;
33+
}
34+
}
35+
}
36+
37+
return -1;
38+
};

0 commit comments

Comments
 (0)