From 3e7983c331e36d25da1e4ecffc535ca7dcf6f838 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Mon, 3 Jun 2024 00:06:55 -0400 Subject: [PATCH 1/5] Add week 6 solutions : findMinimumInRotatedSortedArray --- .../yolophg.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/yolophg.js diff --git a/find-minimum-in-rotated-sorted-array/yolophg.js b/find-minimum-in-rotated-sorted-array/yolophg.js new file mode 100644 index 000000000..6015072ef --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/yolophg.js @@ -0,0 +1,24 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(1) + +var findMin = function(nums) { + // initialize left pointer. + let left = 0; + // initialize right pointer. + let right = nums.length - 1; + + while (left < right) { + // calculate the middle index. + let mid = left + ((right - left) / 2 | 0); + + // if the value at the middle index is bigger, move the left pointer to the right. + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { // else, move the right pointer to the middle index. + right = mid; + } + } + + // return the element which is the minimum pointed to by the left pointer. + return nums[left]; +}; From 0d82c32ccb9197fd0a43e2f52fc9b4212da18c26 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Tue, 4 Jun 2024 19:49:55 -0400 Subject: [PATCH 2/5] Fix typo --- find-minimum-in-rotated-sorted-array/yolophg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/find-minimum-in-rotated-sorted-array/yolophg.js b/find-minimum-in-rotated-sorted-array/yolophg.js index 6015072ef..e5421434d 100644 --- a/find-minimum-in-rotated-sorted-array/yolophg.js +++ b/find-minimum-in-rotated-sorted-array/yolophg.js @@ -1,4 +1,4 @@ -// Time Complexity: O(n log n) +// Time Complexity: O(log n) // Space Complexity: O(1) var findMin = function(nums) { From fbea833ec971fa65dfa1e25d047c3888870c20ef Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Thu, 6 Jun 2024 14:54:29 -0400 Subject: [PATCH 3/5] Add week 6 solutions : containerWithMostWater --- container-with-most-water/yolophg.js | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 container-with-most-water/yolophg.js diff --git a/container-with-most-water/yolophg.js b/container-with-most-water/yolophg.js new file mode 100644 index 000000000..21ffb3174 --- /dev/null +++ b/container-with-most-water/yolophg.js @@ -0,0 +1,32 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var maxArea = function(height) { + let left = 0; + let right = height.length - 1; + + // to store the maximum area. + let maxArea = 0; + + // iterate until the two pointers meet. + while (left < right) { + // calculate the width between the two pointers. + let width = right - left; + + // calculate the area with the current area. + let currentArea = Math.min(height[left], height[right]) * width; + + // update the maximum area if the current area is larger. + maxArea = Math.max(maxArea, currentArea); + + // move the pointer to the shorter line towards the center. + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + // return the maximum area found. + return maxArea; +}; From 54a2f15d62f4f4258e352ec41750e5e27ec05baf Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Fri, 7 Jun 2024 04:41:11 -0400 Subject: [PATCH 4/5] Add week 6 solutions : longestSubstringWithoutRepeatingCharacters, longestRepeatingCharacterReplacement --- .../yolophg.js | 25 ++++++++++++++++ .../yolophg.js | 30 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 longest-repeating-character-replacement/yolophg.js create mode 100644 longest-substring-without-repeating-characters/yolophg.js diff --git a/longest-repeating-character-replacement/yolophg.js b/longest-repeating-character-replacement/yolophg.js new file mode 100644 index 000000000..a2057dc30 --- /dev/null +++ b/longest-repeating-character-replacement/yolophg.js @@ -0,0 +1,25 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var characterReplacement = function(s, k) { + let maxLen = 0; + // initialize an array to store the count of each character. + let charCounts = new Array(26).fill(0); + let start = 0; + + // iterate the string with the end pointer. + for (let end = 0; end < s.length; end++) { + // calculate the index of the character. + let charIndex = s.charCodeAt(end) - 65; + // update maxCount with the maximum count. + maxLen = Math.max(maxLen, ++charCounts[charIndex]); + + // move the start pointer and decrement the count at that position. + if (end - start + 1 - maxLen > k) { + charCounts[s.charCodeAt(start) - 65]--; + start++; + } + } + + return s.length - start; +}; diff --git a/longest-substring-without-repeating-characters/yolophg.js b/longest-substring-without-repeating-characters/yolophg.js new file mode 100644 index 000000000..b8ae5694e --- /dev/null +++ b/longest-substring-without-repeating-characters/yolophg.js @@ -0,0 +1,30 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var lengthOfLongestSubstring = function(s) { + let charSet = new Set(); + + let left = 0; + let right = 0; + + let maxLength = 0; + + // iterate the string. + while (right < s.length) { + if (!charSet.has(s[right])) { + // if the character isn't in the set, add it. + charSet.add(s[right]); + // move the right pointer to the right. + right++; + // update the maximum length if the current window is larger. + maxLength = Math.max(maxLength, right - left); + } else { + // if the character is in the set, remove the leftmost character. + charSet.delete(s[left]); + // move the left pointer to the right. + left++; + } + } + + return maxLength; +}; From 9ddfdfbf48789b29fc64c38b374e6033cb426aea Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Sat, 8 Jun 2024 02:14:36 -0400 Subject: [PATCH 5/5] Add week 6 solutions : searchInRotatedSortedArray --- search-in-rotated-sorted-array/yolophg.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 search-in-rotated-sorted-array/yolophg.js diff --git a/search-in-rotated-sorted-array/yolophg.js b/search-in-rotated-sorted-array/yolophg.js new file mode 100644 index 000000000..29d3bd5a2 --- /dev/null +++ b/search-in-rotated-sorted-array/yolophg.js @@ -0,0 +1,16 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var search = function(nums, target) { + // iterate through each element. + for (let i = 0; i < nums.length; i++) { + // check if the current element is equal to the target. + if (nums[i] === target) { + // if found, return the index. + return i; + } + } + + // if the loop completes without finding the target, return -1. + return -1; +};