Skip to content

[Helena] Week 6 solutions #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions container-with-most-water/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
24 changes: 24 additions & 0 deletions find-minimum-in-rotated-sorted-array/yolophg.js
Original file line number Diff line number Diff line change
@@ -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];
};
25 changes: 25 additions & 0 deletions longest-repeating-character-replacement/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
30 changes: 30 additions & 0 deletions longest-substring-without-repeating-characters/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 16 additions & 0 deletions search-in-rotated-sorted-array/yolophg.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래 정렬되어 있던 배열이 회전되어 있다는 사실이 전혀 활용되지 않고 있어서 조금 아쉬운 것 같네요.

Original file line number Diff line number Diff line change
@@ -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;
};