diff --git a/container-with-most-water/Blossssom.ts b/container-with-most-water/Blossssom.ts new file mode 100644 index 0000000000..6a1fa5eb87 --- /dev/null +++ b/container-with-most-water/Blossssom.ts @@ -0,0 +1,34 @@ +/** + * @param height - 정수 배열 + * @returns - 용기에 담을 수 있는 최대 물의 양 + * @description + * - 양 끝에서 2포인터를 사용해 크기를 체크해 나아감 + * - 결국 거리가 짧아지기 때문에 양쪽 높이를 비교하며 높이가 낮은 포인터를 움직이며 계산 + * - 시간 복잡도 O(n) + */ + +function maxArea(height: number[]): number { + let left = 0; + let right = height.length - 1; + let water = 0; + + while (left < right) { + const calcWater = (right - left) * Math.min(height[left], height[right]); + if (water < calcWater) { + water = calcWater; + } + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return water; +} + +const height = [1, 3, 2, 5, 25, 24, 5]; +const result = maxArea(height); + + diff --git a/design-add-and-search-words-data-structure/Blossssom.ts b/design-add-and-search-words-data-structure/Blossssom.ts new file mode 100644 index 0000000000..016b1e1ae3 --- /dev/null +++ b/design-add-and-search-words-data-structure/Blossssom.ts @@ -0,0 +1,89 @@ +/** + * @class - Dictionary 구현 + * @description + * - .의 경우 모든 문자와 일치 + * - .일 경우 결국 모든 children 조회필요 => dfs + */ + +class WordNode { + children: { [key: string]: WordNode }; + endOfWord: boolean; + constructor() { + this.children = {}; + this.endOfWord = false; + } +} + +class WordDictionary { + private rootNode: WordNode; + constructor() { + this.rootNode = new WordNode(); + } + + addWord(word: string): void { + let nodeInstance = this.rootNode; + + for (const str of word) { + if (!nodeInstance.children[str]) { + nodeInstance.children[str] = new WordNode(); + } + nodeInstance = nodeInstance.children[str]; + } + + nodeInstance.endOfWord = true; + } + + search(word: string): boolean { + return this.searchDfs(this.rootNode, word, 0); + } + + private searchDfs(currentNode: WordNode, word: string, idx: number): boolean { + if (idx === word.length) { + return currentNode.endOfWord; + } + + const char = word[idx]; + if (char === ".") { + for (const key in currentNode.children) { + const childSearch = this.searchDfs( + currentNode.children[key], + word, + idx + 1 + ); + if (childSearch) { + return true; + } + } + + return false; + } else { + if (!currentNode.children[char]) { + return false; + } + + return this.searchDfs(currentNode.children[char], word, idx + 1); + } + } +} + +const wordDict = new WordDictionary(); + +wordDict.addWord("at"); +wordDict.addWord("and"); +wordDict.addWord("an"); +wordDict.addWord("add"); + +wordDict.search("a"); // false +wordDict.search(".at"); // false + +wordDict.addWord("bat"); + +wordDict.search(".at"); // true +wordDict.search("an."); // true +wordDict.search("a.d."); // false +wordDict.search("b."); // false +wordDict.search("a.d"); // true +wordDict.search("."); // false + + + diff --git a/longest-increasing-subsequence/Blossssom.ts b/longest-increasing-subsequence/Blossssom.ts new file mode 100644 index 0000000000..f6c8291d6b --- /dev/null +++ b/longest-increasing-subsequence/Blossssom.ts @@ -0,0 +1,23 @@ +/** + * @param nums - 정수 배열 + * @returns - 엄격하게 증가하는 요소의 길이 반환 + */ +function lengthOfLIS(nums: number[]): number { + const arr: number[] = Array.from({ length: nums.length }, () => 1); + let maxLen = 1; + for (let i = 1; i < nums.length; i++) { + for (let j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + arr[i] = Math.max(arr[i], arr[j] + 1); + } + } + maxLen = Math.max(maxLen, arr[i]); + } + + return maxLen; +} + +const nums = [0, 1, 0, 3, 2, 3]; +lengthOfLIS(nums); + + diff --git a/valid-parentheses/Blossssom.ts b/valid-parentheses/Blossssom.ts new file mode 100644 index 0000000000..d16b03bde1 --- /dev/null +++ b/valid-parentheses/Blossssom.ts @@ -0,0 +1,47 @@ +/** + * @param s - 괄호 문자열 + * @returns - 올바르게 열고 닫혔는지 반환 + */ +// function isValid(s: string): boolean { +// const parentheses: Record = { +// "()": true, +// "{}": true, +// "[]": true, +// }; +// const stack: string[] = []; +// for (const st of s) { +// if ( +// !stack.length || +// parentheses[`${stack[stack.length - 1]}${st}`] === undefined +// ) { +// stack.push(st); +// } else { +// stack.pop(); +// } +// } +// return stack.length ? false : true; +// } + +function isValid(s: string): boolean { + const parentheses: Record = { + "(": ")", + "{": "}", + "[": "]", + }; + + const stack: string[] = []; + for (const char of s) { + if (parentheses[char]) { + stack.push(char); + } else { + const last = stack.pop(); + if (last === undefined || parentheses[last] !== char) { + return false; + } + } + } + + return !stack.length; +} + +