diff --git a/container-with-most-water/hoyeongkwak.ts b/container-with-most-water/hoyeongkwak.ts new file mode 100644 index 000000000..d45b05019 --- /dev/null +++ b/container-with-most-water/hoyeongkwak.ts @@ -0,0 +1,15 @@ +function maxArea(height: number[]): number { + let res = 0 + let l = 0 + let r = height.length - 1 + while (l < r) { + const area = (r - l) * Math.min(height[l], height[r]) + res = Math.max(res, area) + if (height[l] > height[r]) { + r -= 1 + } else { + l += 1 + } + } + return res +}; diff --git a/design-add-and-search-words-data-structure/hoyeongkwak.ts b/design-add-and-search-words-data-structure/hoyeongkwak.ts new file mode 100644 index 000000000..6578f7c80 --- /dev/null +++ b/design-add-and-search-words-data-structure/hoyeongkwak.ts @@ -0,0 +1,63 @@ +/* +O(n) +O(n) +*/ + +class TriedNode { + children: Map + isEndOfWord: boolean + + constructor(){ + this.children = new Map() + this.isEndOfWord = false + } +} + +class WordDictionary { + root: TriedNode + constructor() { + this.root = new TriedNode() + } + + addWord(word: string): void { + let node = this.root + for (const char of word) { + if (!node.children.has(char)) { + node.children.set(char, new TriedNode()) + } + node = node.children.get(char) + } + node.isEndOfWord = true + } + + search(word: string): boolean { + const searchInNode = (word: string, index: number, node: TriedNode): boolean => { + if (index === word.length) return node.isEndOfWord + + const char = word[index] + + if (char === '.') { + for (const [, childNode] of node.children) { + if (searchInNode(word, index + 1, childNode)) { + return true + } + } + return false + } else { + if (!node.children.has(char)) { + return false + } + return searchInNode(word, index + 1, node.children.get(char)) + } + } + return searchInNode(word, 0, this.root) + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * var obj = new WordDictionary() + * obj.addWord(word) + * var param_2 = obj.search(word) + */ + diff --git a/longest-increasing-subsequence/hoyeongkwak.ts b/longest-increasing-subsequence/hoyeongkwak.ts new file mode 100644 index 000000000..c01b65929 --- /dev/null +++ b/longest-increasing-subsequence/hoyeongkwak.ts @@ -0,0 +1,17 @@ +function lengthOfLIS(nums: number[]): number { + let res = 1 + const dp: number[] = Array.from(nums).fill(1) + for (let i = nums.length - 2; i >= 0; i--) { + let curr = 1 + let j = i + while(j < nums.length && curr < res + 1) { + if (nums[j] > nums[i]) { + curr = Math.max(curr, 1 + dp[j]) + } + j++ + } + dp[i] = curr + res = Math.max(dp[i], res) + } + return res +}; diff --git a/spiral-matrix/hoyeongkwak.ts b/spiral-matrix/hoyeongkwak.ts new file mode 100644 index 000000000..a963bd8c9 --- /dev/null +++ b/spiral-matrix/hoyeongkwak.ts @@ -0,0 +1,34 @@ +function spiralOrder(matrix: number[][]): number[] { + const m = matrix.length + const n = matrix[0].length + const size = m * n + let traversed = 0 + let top = 0 + let bottom = m - 1 + let left = 0 + let right = n - 1 + const output = [] + + while (traversed < size) { + for (let i = left; traversed < size && i <= right; i++, traversed++) { + output.push(matrix[top][i]) + } + top++ + + for (let i = top; traversed < size && i <= bottom; i++, traversed++) { + output.push(matrix[i][right]) + } + right-- + + for (let i = right; traversed < size && i >= left; i--, traversed++) { + output.push(matrix[bottom][i]) + } + bottom-- + + for (let i = bottom; traversed < size && i >= top; i--, traversed++) { + output.push(matrix[i][left]) + } + left++ + } + return output +}; diff --git a/valid-parentheses/hoyeongkwak.ts b/valid-parentheses/hoyeongkwak.ts new file mode 100644 index 000000000..f255ccc0c --- /dev/null +++ b/valid-parentheses/hoyeongkwak.ts @@ -0,0 +1,18 @@ +function isValid(s: string): boolean { + const stack: string[] = [] + const strMap = new Map([ + ['(', ')'], + ['[', ']'] , + ['{', '}'] + ]) + for (let str of s) { + if (strMap.has(str)) { + stack.push(strMap.get(str)) + } else if (stack.length > 0 && stack[stack.length - 1] === str) { + stack.pop() + } else { + return false + } + } + return stack.length === 0 +};