-
Notifications
You must be signed in to change notification settings - Fork 126
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
[윤태권] Week6 문제 풀이 #469
Merged
Merged
[윤태권] Week6 문제 풀이 #469
Changes from all commits
Commits
Show all changes
3 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 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,28 @@ | ||
/** | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(1) | ||
* | ||
* 최대로 많은 물을 저장하기 위해 가장 우선적으로 고려해야 하는 것은 짧은 막대의 길이 | ||
* 따라서, 짧은 높이를 가지는 쪽의 포인터를 이동하면서 최대 저장 가능한 값을 비교 | ||
* | ||
*/ | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
int answer = 0; | ||
int left = 0; | ||
int right = height.length - 1; | ||
|
||
while (left < right) { | ||
int water = Math.min(height[left], height[right]) * (right - left); | ||
answer = Math.max(answer, water); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
design-add-and-search-words-data-structure/taekwon-dev.java
This file contains 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,58 @@ | ||
class WordDictionary { | ||
|
||
private class TrieNode { | ||
Map<Character, TrieNode> children = new HashMap<>(); | ||
boolean isEndOfWord = false; | ||
} | ||
|
||
private TrieNode root; | ||
|
||
public WordDictionary() { | ||
root = new TrieNode(); | ||
} | ||
|
||
/** | ||
* 시간 복잡도: O(n), n = 단어의 길이 | ||
* 공간 복잡도: O(n) | ||
*/ | ||
public void addWord(String word) { | ||
TrieNode node = root; | ||
|
||
for (int i = 0; i < word.length(); i++) { | ||
char c = word.charAt(i); | ||
node.children.putIfAbsent(c, new TrieNode()); | ||
node = node.children.get(c); | ||
} | ||
node.isEndOfWord = true; | ||
} | ||
|
||
/** | ||
* 시간 복잡도: O(26^n), n = 단어의 길이 | ||
* 공간 복잡도: O(n) | ||
*/ | ||
public boolean search(String word) { | ||
return searchInNode(word, 0, root); | ||
} | ||
|
||
private boolean searchInNode(String word, int index, TrieNode node) { | ||
if (index == word.length()) { | ||
return node.isEndOfWord; | ||
} | ||
|
||
char c = word.charAt(index); | ||
if (c != '.') { | ||
if (node.children.containsKey(c)) { | ||
return searchInNode(word, index + 1, node.children.get(c)); | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
for (TrieNode childNode : node.children.values()) { | ||
if (searchInNode(word, index + 1, childNode)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains 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 @@ | ||
/** | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(n) | ||
*/ | ||
class Solution { | ||
public boolean isValid(String s) { | ||
Map<Character, Character> map = new HashMap<>(){{ | ||
put(')', '('); | ||
put('}', '{'); | ||
put(']', '['); | ||
}}; | ||
|
||
Deque<Character> stack = new ArrayDeque<>(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
|
||
if (!map.containsKey(c)) { | ||
stack.push(c); | ||
continue; | ||
} | ||
|
||
if (stack.isEmpty() || stack.removeFirst() != map.get(c)) { | ||
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. 제가 Java를 잘 모르는데, stack에서 removeFirst()가 pop()의 역할을 하는걸까요?? 얼핏 드는 생각으로는 정 반대로 맨 앞의 원소를 반환할 것 같아서요 |
||
return false; | ||
} | ||
} | ||
|
||
return stack.size() == 0; | ||
} | ||
} |
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.
중요하지 않은 부분이니 무시하셔도 좋습니다!
어차피 if ~ else ~ 로 양분되는 조건이라면
c != '.'
대신c == '.'
형태로 긍정 조건문을 사용하면 가독성 면에서 좀 더 읽기 쉽지 않을까 싶었습니다!