diff --git a/container-with-most-water/juhui-jeong.ts b/container-with-most-water/juhui-jeong.ts new file mode 100644 index 0000000000..07c4b2b32c --- /dev/null +++ b/container-with-most-water/juhui-jeong.ts @@ -0,0 +1,24 @@ +/* +시간복잡도: O(n) +공간복잡도: O(1) +포인터까지는 생각했으나, 그이후로는 해결법이 떠오르지 않아, 알고달레 풀이를 보고 해결. + +*/ +function maxArea(height: number[]): number { + let maxArea = 0; + let left = 0; + let right = height.length - 1; + + while (left < right) { + let area = (right - left) * Math.min(height[left], height[right]); + + if (maxArea < area) maxArea = area; + + if (height[left] > height[right]) { + right -= 1; + } else { + left += 1; + } + } + return maxArea; +} diff --git a/design-add-and-search-words-data-structure/juhui-jeong.ts b/design-add-and-search-words-data-structure/juhui-jeong.ts new file mode 100644 index 0000000000..b9a892f15b --- /dev/null +++ b/design-add-and-search-words-data-structure/juhui-jeong.ts @@ -0,0 +1,61 @@ +/* +시간복잡도: O(L) +공간복잡도: O(L) +Trie는 검색을 빠르게 해주지만, +. 와일드카드는 DFS 분기를 유발해 최악의 경우 시간 복잡도가 커질 수 있다. + +DFS가 필요한 검색 문제에 대한 공부 필요. +*/ +class TrieNode { + children: Map; + isEnd: boolean; + + constructor() { + this.children = new Map(); + this.isEnd = false; + } +} + +class WordDictionary { + private root: TrieNode; + + constructor() { + this.root = new TrieNode(); + } + + addWord(word: string): void { + let node = this.root; + + for (const ch of word) { + if (!node.children.has(ch)) { + node.children.set(ch, new TrieNode()); + } + node = node.children.get(ch)!; + } + node.isEnd = true; + } + + search(word: string): boolean { + const dfs = (index: number, node: TrieNode): boolean => { + if (index === word.length) { + return node.isEnd; + } + + const ch = word[index]; + + if (ch === '.') { + for (const child of node.children.values()) { + if (dfs(index + 1, child)) return true; + } + return false; + } + + const next = node.children.get(ch); + if (!next) return false; + + return dfs(index + 1, next); + }; + + return dfs(0, this.root); + } +} diff --git a/valid-parentheses/juhui-jeong.ts b/valid-parentheses/juhui-jeong.ts new file mode 100644 index 0000000000..72a47f158a --- /dev/null +++ b/valid-parentheses/juhui-jeong.ts @@ -0,0 +1,35 @@ +/* +시간복잡도: O(n) +공간복잡도: O(n) +*/ +function isValid(s: string): boolean { + if (s.length % 2 !== 0) { + return false; + } + + const stack: string[] = []; + const pair: Map = new Map([ + [')', '('], + ['}', '{'], + [']', '['], + ]); + + for (const char of s) { + // close brackts인지 확인 + if (pair.has(char)) { + let topItem = stack.pop(); + // pair 확인 + if (topItem !== pair.get(char)) { + return false; + } + } else { + stack.push(char); + } + } + + if (stack.length === 0) { + return true; + } else { + return false; + } +}