From 18150bec8590b307cb11514b3a9fcd2710e3dcb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Wed, 18 Sep 2024 22:22:44 +0900 Subject: [PATCH 1/7] Add week 6 solutions: valid-parentheses --- valid-parentheses/gitsunmin.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-parentheses/gitsunmin.ts diff --git a/valid-parentheses/gitsunmin.ts b/valid-parentheses/gitsunmin.ts new file mode 100644 index 000000000..52721221c --- /dev/null +++ b/valid-parentheses/gitsunmin.ts @@ -0,0 +1,25 @@ +/** + * https://leetcode.com/problems/valid-parentheses/ + * time complexity : O(n) + * space complexity : O(n) + */ + +type OpeningBracket = '(' | '[' | '{'; +type ClosingBracket = ')' | ']' | '}'; + +const isEmpty = (stack: OpeningBracket[]): boolean => stack.length === 0; + +function isValid(s: string): boolean { + const m = new Map([ + ['(', ')'], + ['[', ']'], + ['{', '}'] + ]); + const stack: OpeningBracket[] = []; + + for (const c of s) { + if (m.has(c)) stack.push(c as OpeningBracket); + else if (isEmpty(stack) || c !== m.get(stack.pop() as OpeningBracket)) return false; + } + return isEmpty(stack); +}; From 21b5059f6126d4ad1e6a414cab507e225a575cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Wed, 18 Sep 2024 23:11:47 +0900 Subject: [PATCH 2/7] Add week 6 solutions: container-with-most-water --- container-with-most-water/gitsunmin.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 container-with-most-water/gitsunmin.ts diff --git a/container-with-most-water/gitsunmin.ts b/container-with-most-water/gitsunmin.ts new file mode 100644 index 000000000..1bb6dcb90 --- /dev/null +++ b/container-with-most-water/gitsunmin.ts @@ -0,0 +1,15 @@ +/** + * https://leetcode.com/problems/container-with-most-water/ + * time complexity : O(n) + * space complexity : O(1) + */ +export function maxArea(height: number[]): number { + let s = 0, e = height.length - 1, max = 0; + + while (s < e) { + max = Math.max((e - s) * Math.min(height[s], height[e]), max); + if (height[s] < height[e]) s++; + else e--; + } + return max; +}; From 83828870c2897a3647d00e64a3e99676e0351dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Thu, 19 Sep 2024 00:04:23 +0900 Subject: [PATCH 3/7] Add week 6 solutions: design-add-and-search-words-data-structure --- .../gitsunmin.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 design-add-and-search-words-data-structure/gitsunmin.ts diff --git a/design-add-and-search-words-data-structure/gitsunmin.ts b/design-add-and-search-words-data-structure/gitsunmin.ts new file mode 100644 index 000000000..89227219f --- /dev/null +++ b/design-add-and-search-words-data-structure/gitsunmin.ts @@ -0,0 +1,58 @@ +/** + * https://leetcode.com/problems/design-add-and-search-words-data-structure + * n: The total number of words stored in the Trie. + * m: The average length of each word. + * + * time complexity: + * - addWord: O(m) + * - search: O(26^m) in the worst case (with multiple wildcards) to O(m) in general cases. + * space complexity: O(n * m) + */ +class TrieNode { + children: Map; + isEnd: boolean; + + constructor() { + this.children = new Map(); + this.isEnd = false; + } +} + +export class WordDictionary { + private root: TrieNode; + + constructor() { + this.root = new TrieNode(); + } + + addWord(word: string): void { + let node = this.root; + + for (const char of word) { + if (!node.children.has(char)) node.children.set(char, new TrieNode()); + + node = node.children.get(char)!; + } + node.isEnd = true; + } + + search(word: string): boolean { + return this.searchInNode(word, 0, this.root); + } + + private searchInNode(word: string, index: number, node: TrieNode): boolean { + if (index === word.length) return node.isEnd; + + const char = word[index]; + + if (char === '.') { + for (const child of node.children.values()) { + if (this.searchInNode(word, index + 1, child)) return true; + } + return false; + } else { + if (!node.children.has(char)) return false; + return this.searchInNode(word, index + 1, node.children.get(char)!); + } + } +} From 36e7cf3375d842abfecd831d0aaeb2edd90bd0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 21 Sep 2024 15:53:30 +0900 Subject: [PATCH 4/7] Add week 6 solutions: longest-increasing-subsequence --- longest-increasing-subsequence/gitsunmin.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-increasing-subsequence/gitsunmin.ts diff --git a/longest-increasing-subsequence/gitsunmin.ts b/longest-increasing-subsequence/gitsunmin.ts new file mode 100644 index 000000000..8ca85876f --- /dev/null +++ b/longest-increasing-subsequence/gitsunmin.ts @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/longest-increasing-subsequence + * time complexity : O(n) + * space complexity : O(n) + */ +function lengthOfLIS(nums: number[]): number { + const [head] = nums; + const basket = [head]; + + for (let i = 1; i < nums.length; i++) { + const current = nums[i]; + let j = 0; + + while (j < basket.length && basket[j] < current) j++; + + if (j === basket.length) basket.push(current); + else basket[j] = current; + } + + return basket.length; +}; From 36743b0d5666258ed6a5df5754d8a9a0895e5076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 21 Sep 2024 16:26:52 +0900 Subject: [PATCH 5/7] Add week 6 solutions: spiral-matrix --- spiral-matrix/gitsunmin.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 spiral-matrix/gitsunmin.ts diff --git a/spiral-matrix/gitsunmin.ts b/spiral-matrix/gitsunmin.ts new file mode 100644 index 000000000..40dbd77e7 --- /dev/null +++ b/spiral-matrix/gitsunmin.ts @@ -0,0 +1,32 @@ +/** + * https://leetcode.com/problems/spiral-matrix/ + * time complexity : O(m * n) + * space complexity : O(m * n) + */ + +function spiralOrder(matrix: number[][]): number[] { + let [left, right] = [0, matrix[0].length - 1]; + let [top, bottom] = [0, matrix.length - 1]; + + const output = [] as number[]; + + while (top <= bottom && left <= right) { + for (let i = left; i <= right; i++) output.push(matrix[top][i]); + top++; + + for (let i = top; i <= bottom; i++) output.push(matrix[i][right]); + right--; + + if (top <= bottom) { + for (let i = right; i >= left; i--) output.push(matrix[bottom][i]); + bottom--; + } + + if (left <= right) { + for (let i = bottom; i >= top; i--) output.push(matrix[i][left]); + left++; + } + } + + return output; +}; From d531d5e36aa2fc1d49fcaeb7f91adbbad05e5320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 22 Sep 2024 16:22:03 +0900 Subject: [PATCH 6/7] update comment on design-add-and-search-words-data-structure --- design-add-and-search-words-data-structure/gitsunmin.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/gitsunmin.ts b/design-add-and-search-words-data-structure/gitsunmin.ts index 89227219f..3ae6a764f 100644 --- a/design-add-and-search-words-data-structure/gitsunmin.ts +++ b/design-add-and-search-words-data-structure/gitsunmin.ts @@ -6,7 +6,9 @@ * time complexity: * - addWord: O(m) * - search: O(26^m) in the worst case (with multiple wildcards) to O(m) in general cases. - * space complexity: O(n * m) + * space complexity: + * - addWord: O(n * m) + * - search: O(n * m) */ class TrieNode { children: Map; From 681a47da0f32dcb34865caca9eaade62776c1246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 22 Sep 2024 16:27:47 +0900 Subject: [PATCH 7/7] update time complexity on longest-increasing-subsequence --- longest-increasing-subsequence/gitsunmin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-increasing-subsequence/gitsunmin.ts b/longest-increasing-subsequence/gitsunmin.ts index 8ca85876f..8ee9fb0f1 100644 --- a/longest-increasing-subsequence/gitsunmin.ts +++ b/longest-increasing-subsequence/gitsunmin.ts @@ -1,6 +1,6 @@ /** * https://leetcode.com/problems/longest-increasing-subsequence - * time complexity : O(n) + * time complexity : O(n²) * space complexity : O(n) */ function lengthOfLIS(nums: number[]): number {