From 46d6fc99fd98d2bd51bdd657ed56e1583d8c91c1 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 21 Sep 2024 00:34:00 +0900 Subject: [PATCH 1/7] 1. Valid Parentheses --- valid-parentheses/whewchews.ts | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 valid-parentheses/whewchews.ts diff --git a/valid-parentheses/whewchews.ts b/valid-parentheses/whewchews.ts new file mode 100644 index 000000000..275a13887 --- /dev/null +++ b/valid-parentheses/whewchews.ts @@ -0,0 +1,38 @@ +/* + * 아이디어 + * stack 자료구조를 사용해 여는 괄호가 나오면 순서대로 저장해둔다. + * stack을 사용하는 이유는 가장 최근 여는 괄호가 어떤 것인지 확인하기 위함이다.(마지막에 삽입한 값을 먼저 사용함) + * 닫는 괄호가 나오면 stack의 마지막 값이 pair인 여는 괄호인지 체크한다. 다르면 return false + * 문자열 반복문을 다 돌고 stack에 여는 괄호가 남아있지 않은지 본다. 남아있으면 return false + * 위의 두 경우에 해당되지 않으면 return true + */ +function isValid(s: string): boolean { + const charSet = new Set(["(", "{", "["]); + const openCharStack = []; + const CHAR_PAIR = { + "]": "[", + "}": "{", + ")": "(", + }; + + for (let i = 0; i <= s.length - 1; i++) { + const char = s[i]; + if (charSet.has(char)) { + openCharStack.push(char); + continue; + } + + if (char in CHAR_PAIR) { + const pair = CHAR_PAIR[char]; + const lastOpenChar = openCharStack.pop(); + if (lastOpenChar !== pair) { + return false; + } + } + } + + const isEmpty = openCharStack.length === 0; + return isEmpty; +} +// TC: O(n) +// SC: O(n) From b8fa39b9e2e6b4595d181cea7233dd8fa23e8d94 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 21 Sep 2024 00:58:43 +0900 Subject: [PATCH 2/7] 2. Container With Most Water --- container-with-most-water/whewchews.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 container-with-most-water/whewchews.ts diff --git a/container-with-most-water/whewchews.ts b/container-with-most-water/whewchews.ts new file mode 100644 index 000000000..867ac18ef --- /dev/null +++ b/container-with-most-water/whewchews.ts @@ -0,0 +1,24 @@ +function maxArea(height: number[]): number { + let left = 0; + let right = height.length - 1; + let maxSize = 0; + + while (left < right) { + maxSize = Math.max(maxSize, getMaxSize(height, left, right)); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxSize; +} + +function getMaxSize(height: number[], left: number, right: number) { + return Math.min(...[height[right], height[left]]) * (right - left); +} + +// TC: O(n) +// SC: O(1) From 8a42f7d264ecb1085ab2d717cb48b41e2404133e Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 21 Sep 2024 02:34:03 +0900 Subject: [PATCH 3/7] 3. Design Add and Search Words Data Structure --- .../whewchews.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 design-add-and-search-words-data-structure/whewchews.ts diff --git a/design-add-and-search-words-data-structure/whewchews.ts b/design-add-and-search-words-data-structure/whewchews.ts new file mode 100644 index 000000000..28961739c --- /dev/null +++ b/design-add-and-search-words-data-structure/whewchews.ts @@ -0,0 +1,48 @@ +class WordDictionary { + wordList: Set; + wordCountMap: Map; + constructor() { + this.wordList = new Set(); + this.wordCountMap = new Map(); + } + + // TC: O(1) + // SC: O(n) + addWord(word: string): void { + this.wordList.add(word); + const length = word.length; + if (this.wordCountMap.has(length)) { + this.wordCountMap.get(length).push(word); + } else { + this.wordCountMap.set(length, [word]); + } + return null; + } + + // TC: O(m*n) // m: words length, n: word length + // SC: O(1) + search(word: string): boolean { + const len = word.length; + const targetWord = word.replace(/\./g, ""); + const hasDot = len - targetWord.length !== 0; + + if (!hasDot) return this.wordList.has(word); + if (!this.wordCountMap.has(len)) { + return false; + } + const words = this.wordCountMap.get(len); + for (let i = 0; i < words.length; i++) { + let match = true; + for (let j = 0; j < words[i].length; j++) { + if (word[j] !== "." && word[j] !== words[i][j]) { + match = false; + break; + } + } + if (match) { + return true; + } + } + return false; + } +} From f6cf44e0171581591b96779202d9b188263bf740 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 21 Sep 2024 02:49:18 +0900 Subject: [PATCH 4/7] 5. Spiral Matrix --- spiral-matrix/whewchews.ts | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spiral-matrix/whewchews.ts diff --git a/spiral-matrix/whewchews.ts b/spiral-matrix/whewchews.ts new file mode 100644 index 000000000..3748d9436 --- /dev/null +++ b/spiral-matrix/whewchews.ts @@ -0,0 +1,39 @@ +function spiralOrder(matrix: number[][]): number[] { + const rows = matrix.length; + const cols = matrix[0].length; + const total = rows * cols; + let srow = 0; // start row + let scol = 0; + let erow = rows - 1; // end row + let ecol = cols - 1; + let count = 0; + const ans: number[] = []; + + while (count < total) { + for (let i = scol; i <= ecol && count < total; i++) { + ans.push(matrix[srow][i]); + count++; + } + srow++; + for (let i = srow; i <= erow && count < total; i++) { + ans.push(matrix[i][ecol]); + count++; + } + ecol--; + for (let i = ecol; i >= scol && count < total; i--) { + ans.push(matrix[erow][i]); + count++; + } + erow--; + for (let i = erow; i >= srow && count < total; i--) { + ans.push(matrix[i][scol]); + count++; + } + scol++; + } + + return ans; +} + +// TC: O(m*n) +// SC: O(m*n) From 6e0e5b354878bf0c05bcdf95a2da951142a39e69 Mon Sep 17 00:00:00 2001 From: whewchews <111553718+whewchews@users.noreply.github.com> Date: Mon, 23 Sep 2024 00:19:30 +0900 Subject: [PATCH 5/7] refactor: remove charSet Co-authored-by: Dongyeong Chon --- valid-parentheses/whewchews.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/valid-parentheses/whewchews.ts b/valid-parentheses/whewchews.ts index 275a13887..aae0ef9ae 100644 --- a/valid-parentheses/whewchews.ts +++ b/valid-parentheses/whewchews.ts @@ -7,7 +7,6 @@ * 위의 두 경우에 해당되지 않으면 return true */ function isValid(s: string): boolean { - const charSet = new Set(["(", "{", "["]); const openCharStack = []; const CHAR_PAIR = { "]": "[", @@ -17,10 +16,6 @@ function isValid(s: string): boolean { for (let i = 0; i <= s.length - 1; i++) { const char = s[i]; - if (charSet.has(char)) { - openCharStack.push(char); - continue; - } if (char in CHAR_PAIR) { const pair = CHAR_PAIR[char]; @@ -28,6 +23,8 @@ function isValid(s: string): boolean { if (lastOpenChar !== pair) { return false; } + } else { + openCharStack.push(char); } } From 73a3a2d074ebe37e66d5f9cd347b03f7a8f58ef4 Mon Sep 17 00:00:00 2001 From: whewchews Date: Mon, 23 Sep 2024 00:28:27 +0900 Subject: [PATCH 6/7] fix: space complexity calculation in search method --- design-add-and-search-words-data-structure/whewchews.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/whewchews.ts b/design-add-and-search-words-data-structure/whewchews.ts index 28961739c..a86d5b531 100644 --- a/design-add-and-search-words-data-structure/whewchews.ts +++ b/design-add-and-search-words-data-structure/whewchews.ts @@ -20,7 +20,7 @@ class WordDictionary { } // TC: O(m*n) // m: words length, n: word length - // SC: O(1) + // SC: O(n) search(word: string): boolean { const len = word.length; const targetWord = word.replace(/\./g, ""); From 28876edbadb0502790e1d9d762da4abade103ff1 Mon Sep 17 00:00:00 2001 From: whewchews Date: Mon, 23 Sep 2024 00:33:55 +0900 Subject: [PATCH 7/7] refactor: replace wordList with wordCountMap --- .../whewchews.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/design-add-and-search-words-data-structure/whewchews.ts b/design-add-and-search-words-data-structure/whewchews.ts index a86d5b531..0023e6b50 100644 --- a/design-add-and-search-words-data-structure/whewchews.ts +++ b/design-add-and-search-words-data-structure/whewchews.ts @@ -1,20 +1,17 @@ class WordDictionary { - wordList: Set; - wordCountMap: Map; + wordCountMap: Map>; constructor() { - this.wordList = new Set(); this.wordCountMap = new Map(); } // TC: O(1) // SC: O(n) addWord(word: string): void { - this.wordList.add(word); const length = word.length; if (this.wordCountMap.has(length)) { - this.wordCountMap.get(length).push(word); + this.wordCountMap.get(length).add(word); } else { - this.wordCountMap.set(length, [word]); + this.wordCountMap.set(length, new Set([word])); } return null; } @@ -26,15 +23,18 @@ class WordDictionary { const targetWord = word.replace(/\./g, ""); const hasDot = len - targetWord.length !== 0; - if (!hasDot) return this.wordList.has(word); if (!this.wordCountMap.has(len)) { return false; } const words = this.wordCountMap.get(len); - for (let i = 0; i < words.length; i++) { + if (!hasDot) { + return words.has(word); + } + + for (const w of words) { let match = true; - for (let j = 0; j < words[i].length; j++) { - if (word[j] !== "." && word[j] !== words[i][j]) { + for (let j = 0; j < w.length; j++) { + if (word[j] !== "." && word[j] !== w[j]) { match = false; break; }