diff --git a/container-with-most-water/hyer0705.ts b/container-with-most-water/hyer0705.ts new file mode 100644 index 000000000..8ecf2be2a --- /dev/null +++ b/container-with-most-water/hyer0705.ts @@ -0,0 +1,21 @@ +function maxArea(height: number[]): number { + const n = height.length; + + let maximumArea = 0; + let l = 0; + let r = n - 1; + + while (l < r) { + const currentArea = (r - l) * Math.min(height[l], height[r]); + + maximumArea = Math.max(maximumArea, currentArea); + + if (height[l] > height[r]) { + r--; + } else { + l++; + } + } + + return maximumArea; +} diff --git a/design-add-and-search-words-data-structure/hyer0705.ts b/design-add-and-search-words-data-structure/hyer0705.ts new file mode 100644 index 000000000..497a435e1 --- /dev/null +++ b/design-add-and-search-words-data-structure/hyer0705.ts @@ -0,0 +1,68 @@ +class TNode { + isEndOf: boolean; + children: Map; + + constructor() { + this.isEndOf = false; + this.children = new Map(); + } +} + +class WordDictionary { + private root: TNode; + + constructor() { + this.root = new TNode(); + } + + addWord(word: string): void { + let current = this.root; + + for (const ch of word) { + if (!current.children.has(ch)) { + current.children.set(ch, new TNode()); + } + + current = current.children.get(ch)!; + } + + current.isEndOf = true; + } + + search(word: string): boolean { + const find = (node: TNode, index: number): boolean => { + if (index === word.length) { + if (node.isEndOf) return true; + return false; + } + + const currentCh = word[index]; + + if (currentCh === ".") { + for (const [ch, child] of node.children) { + if (find(child, index + 1)) return true; + } + + return false; + } else { + const child = node.children.get(currentCh); + if (child) { + return find(child, index + 1); + } + + return false; + } + + return false; + }; + + return find(this.root, 0); + } +} + +/** + * 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/hyer0705.ts b/longest-increasing-subsequence/hyer0705.ts new file mode 100644 index 000000000..c5e73ea69 --- /dev/null +++ b/longest-increasing-subsequence/hyer0705.ts @@ -0,0 +1,44 @@ +// using binary search, lower bound +function lengthOfLIS(nums: number[]): number { + const n = nums.length; + const sub: number[] = []; + + for (const num of nums) { + if (sub.length === 0 || num > sub[sub.length - 1]) { + sub.push(num); + } else if (num <= sub[sub.length - 1]) { + let l = 0; + let r = sub.length - 1; + + while (l < r) { + const mid = Math.floor((l + r) / 2); + + if (num <= sub[mid]) { + r = mid; + } else { + l = mid + 1; + } + } + + sub[l] = num; + } + } + + return sub.length; +} + +// using dp +function lengthOfLIS(nums: number[]): number { + const n = nums.length; + const dp: number[] = Array(n).fill(1); + + for (let i = 1; i < n; i++) { + for (let j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + return Math.max(...dp); +} diff --git a/spiral-matrix/hyer0705.ts b/spiral-matrix/hyer0705.ts new file mode 100644 index 000000000..eb98a69be --- /dev/null +++ b/spiral-matrix/hyer0705.ts @@ -0,0 +1,38 @@ +function spiralOrder(matrix: number[][]): number[] { + const m = matrix.length; + const n = matrix[0].length; + + const spiral: number[] = []; + + let top = 0; + let bottom = m - 1; + let left = 0; + let right = n - 1; + + // right -> bottom -> left -> top + while (left <= right && top <= bottom) { + for (let i = left; i <= right; i++) { + spiral.push(matrix[top][i]); + } + top++; + + for (let i = top; i <= bottom; i++) { + spiral.push(matrix[i][right]); + } + right--; + + if (left <= right && top <= bottom) { + for (let i = right; i >= left; i--) { + spiral.push(matrix[bottom][i]); + } + bottom--; + + for (let i = bottom; i >= top; i--) { + spiral.push(matrix[i][left]); + } + left++; + } + } + + return spiral; +} diff --git a/valid-parentheses/hyer0705.ts b/valid-parentheses/hyer0705.ts new file mode 100644 index 000000000..3a77d7d09 --- /dev/null +++ b/valid-parentheses/hyer0705.ts @@ -0,0 +1,20 @@ +function isValid(s: string): boolean { + const stack: string[] = []; + + const matchMap = new Map(); + matchMap.set(")", "("); + matchMap.set("]", "["); + matchMap.set("}", "{"); + + for (const bracket of s) { + if (matchMap.has(bracket)) { + if (stack.length === 0 || matchMap.get(bracket) !== stack.pop()) { + return false; + } + } else { + stack.push(bracket); + } + } + + return stack.length === 0; +}