-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e7983c
Add week 6 solutions : findMinimumInRotatedSortedArray
yolophg 810602d
Merge branch 'DaleStudy:main' into main
yolophg 0d82c32
Fix typo
yolophg fbea833
Add week 6 solutions : containerWithMostWater
yolophg 54a2f15
Add week 6 solutions : longestSubstringWithoutRepeatingCharacters, lo…
yolophg 496d9e4
Merge branch 'DaleStudy:main' into main
yolophg 9ddfdfb
Add week 6 solutions : searchInRotatedSortedArray
yolophg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
원래 정렬되어 있던 배열이 회전되어 있다는 사실이 전혀 활용되지 않고 있어서 조금 아쉬운 것 같네요.