-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Jeehay28] WEEK 06 #891
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
[Jeehay28] WEEK 06 #891
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7287a87
Add container-with-most-water solution
Jeehay28 b2a6a85
Add valid-parentheses solution
Jeehay28 50b3ab9
Add design-add-and-search-words-data-structure solution
Jeehay28 b64d18b
Add spiral-matrix solution
Jeehay28 ec041f9
Add longest-increasing-subsequence solution
Jeehay28 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,29 @@ | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
|
||
// Time Complexity: O(n) | ||
// Space Complexity: O(1) | ||
var maxArea = function (height) { | ||
let start = 0; | ||
let end = height.length - 1; | ||
let maxArea = 0; | ||
|
||
while (start < end) { | ||
const area = (end - start) * Math.min(height[start], height[end]); | ||
maxArea = Math.max(area, maxArea); | ||
|
||
// The shorter height limits the area. | ||
// By moving the pointer associated with the shorter height, | ||
// the algorithm maximizes the chance of finding a taller line that can increase the area. | ||
// This is the essence of the two-pointer strategy for the container problem. | ||
if (height[start] < height[end]) { | ||
start += 1; | ||
} else { | ||
end -= 1; | ||
} | ||
} | ||
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,35 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
|
||
// Time Complexity: O(n) | ||
// Space Complexity: O(n) | ||
var isValid = function (s) { | ||
const obj = { | ||
"(" : ")", | ||
"{" : "}", | ||
"[" : "]", | ||
}; | ||
|
||
let stack = []; | ||
|
||
for (any of s) { | ||
// open bracket | ||
if (obj[any]) { | ||
stack.push(any); | ||
} else { | ||
// close bracket | ||
if (stack.length === 0) { | ||
return false; | ||
} else if (obj[stack[stack.length - 1]] !== any) { | ||
return false; | ||
} else { | ||
stack.pop(); | ||
} | ||
} | ||
} | ||
return stack.length === 0 ? true : false; | ||
}; | ||
|
||
|
Oops, something went wrong.
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.
return stack.length === 0
만 해도 true, false 로 반환되지 않을까요? :)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.
아, 그렇네요. 감사합니다.