diff --git a/container-with-most-water/hoyeongkwak.java b/container-with-most-water/hoyeongkwak.java new file mode 100644 index 000000000..49f0711d8 --- /dev/null +++ b/container-with-most-water/hoyeongkwak.java @@ -0,0 +1,22 @@ +/* +Time Complexity : O(n) +Space Complexity : O(1) +*/ +class Solution { + public int maxArea(int[] height) { + int left = 0; + int right = height.length - 1; + int area = 0; + + while (left < right) { + int currentArea = (right - left) * Math.min(height[left], height[right]); + area = Math.max(area, currentArea); + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + return area; + } +} diff --git a/design-add-and-search-words-data-structure/hoyeongkwak.java b/design-add-and-search-words-data-structure/hoyeongkwak.java new file mode 100644 index 000000000..c6b52420a --- /dev/null +++ b/design-add-and-search-words-data-structure/hoyeongkwak.java @@ -0,0 +1,72 @@ +/* +m : word length +n : Trie node count +addWord +Time Complexity: O(m) + +search +Time Complexity: O(n) + +Space Complexity: O(26 × N × M) + +Trie + Dfs +*/ + +class WordDictionary { + class TrieNode { + TrieNode[] children; + boolean isEnd; + + public TrieNode() { + children = new TrieNode[26]; + isEnd = false; + } + } + + private TrieNode root; + + public WordDictionary() { + root = new TrieNode(); + } + + public void addWord(String word) { + TrieNode current = root; + + for (char str : word.toCharArray()) { + int idx = str - 'a'; + if (current.children[idx] == null) { + current.children[idx] = new TrieNode(); + } + current = current.children[idx]; + } + current.isEnd = true; + } + + public boolean search(String word) { + return dfsSearch(word, 0, root); + } + + private boolean dfsSearch(String word, int idx, TrieNode node) { + if (idx == word.length()) { + return node.isEnd; + } + + char c = word.charAt(idx); + if (c == '.') { + for (int i = 0 ; i < 26; i++) { + if (node.children[i] != null) { + if (dfsSearch(word, idx + 1, node.children[i])) { + return true; + } + } + } + return false; + } else { + int charIdx = c - 'a'; + if (node.children[charIdx] == null) { + return false; + } + return dfsSearch(word, idx + 1, node.children[charIdx]); + } + } +} diff --git a/valid-parentheses/hoyeongkwak.java b/valid-parentheses/hoyeongkwak.java new file mode 100644 index 000000000..1e420a278 --- /dev/null +++ b/valid-parentheses/hoyeongkwak.java @@ -0,0 +1,24 @@ +class Solution { + public boolean isValid(String s) { + if (s == null || s.length() % 2 != 0) { + return false; + } + Stack stack = new Stack<>(); + HashMap strList = new HashMap<>(); + strList.put(')', '('); + strList.put(']', '['); + strList.put('}', '{'); + + for (char c : s.toCharArray()) { + if (strList.containsKey(c)) { + if (stack.isEmpty() || stack.pop() != strList.get(c)) { + return false; + } + } else { + stack.push(c); + } + } + + return stack.isEmpty(); + } +}