Skip to content
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 3 commits into from
Sep 22, 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
28 changes: 28 additions & 0 deletions container-with-most-water/taekwon-dev.java
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 design-add-and-search-words-data-structure/taekwon-dev.java
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 != '.') {
Copy link
Contributor

Choose a reason for hiding this comment

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

중요하지 않은 부분이니 무시하셔도 좋습니다!
어차피 if ~ else ~ 로 양분되는 조건이라면 c != '.' 대신 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;
}
}
}
30 changes: 30 additions & 0 deletions valid-parentheses/taekwon-dev.java
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

제가 Java를 잘 모르는데, stack에서 removeFirst()가 pop()의 역할을 하는걸까요?? 얼핏 드는 생각으로는 정 반대로 맨 앞의 원소를 반환할 것 같아서요

return false;
}
}

return stack.size() == 0;
}
}