diff --git a/container-with-most-water/whewchews.ts b/container-with-most-water/whewchews.ts new file mode 100644 index 00000000..867ac18e --- /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) 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 00000000..0023e6b5 --- /dev/null +++ b/design-add-and-search-words-data-structure/whewchews.ts @@ -0,0 +1,48 @@ +class WordDictionary { + wordCountMap: Map>; + constructor() { + this.wordCountMap = new Map(); + } + + // TC: O(1) + // SC: O(n) + addWord(word: string): void { + const length = word.length; + if (this.wordCountMap.has(length)) { + this.wordCountMap.get(length).add(word); + } else { + this.wordCountMap.set(length, new Set([word])); + } + return null; + } + + // TC: O(m*n) // m: words length, n: word length + // SC: O(n) + search(word: string): boolean { + const len = word.length; + const targetWord = word.replace(/\./g, ""); + const hasDot = len - targetWord.length !== 0; + + if (!this.wordCountMap.has(len)) { + return false; + } + const words = this.wordCountMap.get(len); + if (!hasDot) { + return words.has(word); + } + + for (const w of words) { + let match = true; + for (let j = 0; j < w.length; j++) { + if (word[j] !== "." && word[j] !== w[j]) { + match = false; + break; + } + } + if (match) { + return true; + } + } + return false; + } +} diff --git a/spiral-matrix/whewchews.ts b/spiral-matrix/whewchews.ts new file mode 100644 index 00000000..3748d943 --- /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) diff --git a/valid-parentheses/whewchews.ts b/valid-parentheses/whewchews.ts new file mode 100644 index 00000000..aae0ef9a --- /dev/null +++ b/valid-parentheses/whewchews.ts @@ -0,0 +1,35 @@ +/* + * 아이디어 + * stack 자료구조를 사용해 여는 괄호가 나오면 순서대로 저장해둔다. + * stack을 사용하는 이유는 가장 최근 여는 괄호가 어떤 것인지 확인하기 위함이다.(마지막에 삽입한 값을 먼저 사용함) + * 닫는 괄호가 나오면 stack의 마지막 값이 pair인 여는 괄호인지 체크한다. 다르면 return false + * 문자열 반복문을 다 돌고 stack에 여는 괄호가 남아있지 않은지 본다. 남아있으면 return false + * 위의 두 경우에 해당되지 않으면 return true + */ +function isValid(s: string): boolean { + const openCharStack = []; + const CHAR_PAIR = { + "]": "[", + "}": "{", + ")": "(", + }; + + for (let i = 0; i <= s.length - 1; i++) { + const char = s[i]; + + if (char in CHAR_PAIR) { + const pair = CHAR_PAIR[char]; + const lastOpenChar = openCharStack.pop(); + if (lastOpenChar !== pair) { + return false; + } + } else { + openCharStack.push(char); + } + } + + const isEmpty = openCharStack.length === 0; + return isEmpty; +} +// TC: O(n) +// SC: O(n)