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; +}; 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..e5421434d --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/yolophg.js @@ -0,0 +1,24 @@ +// Time Complexity: O(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]; +}; 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; +}; 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; +};