From c6811d7d769d07d3121e2292bd51b5b48fcc8dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Tue, 26 Aug 2025 22:42:53 +0900 Subject: [PATCH 1/6] valid parentheses solution --- valid-parentheses/hyer0705.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 valid-parentheses/hyer0705.ts 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; +} From ff1251b5fd8a123ce665d14de7fe71097571912f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Thu, 28 Aug 2025 17:07:48 +0900 Subject: [PATCH 2/6] container with most water solution --- container-with-most-water/hyer0705.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 container-with-most-water/hyer0705.ts 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; +} From 036e221a81d0607842f276a180fd7fe77f352166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Thu, 28 Aug 2025 17:45:01 +0900 Subject: [PATCH 3/6] design add and search words data structure solution --- .../hyer0705.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 design-add-and-search-words-data-structure/hyer0705.ts 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) + */ From e53c316de7fe251240b92f080676db93e01aae8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Sat, 30 Aug 2025 07:10:31 +0900 Subject: [PATCH 4/6] longest increasing subsequence solution --- longest-increasing-subsequence/hyer0705.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 longest-increasing-subsequence/hyer0705.ts diff --git a/longest-increasing-subsequence/hyer0705.ts b/longest-increasing-subsequence/hyer0705.ts new file mode 100644 index 000000000..e1f2693d5 --- /dev/null +++ b/longest-increasing-subsequence/hyer0705.ts @@ -0,0 +1,14 @@ +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); +} From 61e16db7d141081ac594e93ef8b6a6e9926afaa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Sat, 30 Aug 2025 07:21:40 +0900 Subject: [PATCH 5/6] longest increasing subsequence solution - using binary search, lower bound --- longest-increasing-subsequence/hyer0705.ts | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/longest-increasing-subsequence/hyer0705.ts b/longest-increasing-subsequence/hyer0705.ts index e1f2693d5..c5e73ea69 100644 --- a/longest-increasing-subsequence/hyer0705.ts +++ b/longest-increasing-subsequence/hyer0705.ts @@ -1,3 +1,33 @@ +// 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); From 54bab01c3cb73d3d9c4c7835149e1ef6a898b893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A9=E1=84=92=E1=85=A8=E1=84=85=E1=85=B5?= =?UTF-8?q?=E1=86=AB?= Date: Sat, 30 Aug 2025 07:46:59 +0900 Subject: [PATCH 6/6] spiral matrix solution --- spiral-matrix/hyer0705.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 spiral-matrix/hyer0705.ts 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; +}