-
-
Notifications
You must be signed in to change notification settings - Fork 195
[GangBean] Week 6 #884
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
[GangBean] Week 6 #884
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,45 @@ | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
/** | ||
1. understanding | ||
- with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water | ||
- find maximum amount of water can contain | ||
2. strategy | ||
- brute force | ||
- for each pair of lines, calculate amount and update maximum amount. | ||
- it can takes O(N), where N is the count of lines. | ||
- N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds | ||
- you need to swap out unnecessary calculation | ||
- so, let's find a unnecessary calculation in this problem. | ||
3. complexity | ||
- time: O(N) | ||
- space: O(1) | ||
*/ | ||
int l = 0; | ||
int r = height.length - 1; | ||
int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l); | ||
while (l < r) { // O(N) | ||
maxAmount = Math.max(maxAmount, amountOf(height, l, r)); | ||
if (height[l] < height[r]) { | ||
l++; | ||
} else if (height[l] > height[r]) { | ||
r--; | ||
} else { | ||
int nextLeftAmount = amountOf(height, l+1, r); | ||
int nextRightAmount = amountOf(height, l, r-1); | ||
if (nextLeftAmount < nextRightAmount) { | ||
r--; | ||
} else { | ||
l++; | ||
} | ||
} | ||
} | ||
|
||
return maxAmount; | ||
} | ||
|
||
private int amountOf(int[] height, int left, int right) { | ||
return (Math.min(height[left], height[right]) * (right - 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,38 @@ | ||
class Solution { | ||
public boolean isValid(String s) { | ||
/** | ||
1. understanding | ||
- given string contains character which has paired characters. | ||
- validate all given characters in input string has each pair, and valid in order. | ||
2. strategy | ||
- use stack, to save left brackets, and if you encounter right bracket, then pop from stack, and compare two characters are paired. | ||
- if not paired on any right character, or stack is empty then return false. | ||
- all characters are iterated and stack is not empty, then return false. | ||
3. complexity | ||
- time: O(N), N is the length of input string s. | ||
- space: O(N), for stack variable memory. | ||
*/ | ||
Stack<Character> leftBracket = new Stack<>(); | ||
for (char c : s.toCharArray()) { | ||
if (c == '(' || c == '{' || c == '[') { | ||
// when character is left bracket, then push to stack. | ||
leftBracket.push(c); | ||
} else if (c == ')' || c == '}' || c == ']') { | ||
if (leftBracket.isEmpty()) return false; | ||
char left = leftBracket.pop(); | ||
if (isPair(left, c)) continue; | ||
return false; | ||
} else { | ||
throw new RuntimeException(String.format("Not valid input character: %c in input %s", c, s)); | ||
} | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리 보다는 false를 바로 던지시면 되지 않을까요? |
||
} | ||
return leftBracket.isEmpty(); | ||
} | ||
|
||
private boolean isPair(char left, char right) { | ||
return (left == '(' && right == ')') | ||
|| (left == '{' && right == '}') | ||
|| (left == '[' && right == ']'); | ||
} | ||
} | ||
|
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.
투 포인터가 줄어들 때 마다, 전체 면적이 줄어 들 때 내부 while 문을 사용하면 시간을 좀 더 효율적으로 가져갈 수도 있을것 같아요 :)