diff --git a/3sum/hyer0705.ts b/3sum/hyer0705.ts new file mode 100644 index 000000000..d1bbcee76 --- /dev/null +++ b/3sum/hyer0705.ts @@ -0,0 +1,31 @@ +function threeSum(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + + const result: number[][] = []; + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + const target = -nums[i]; + + let j = i + 1; + let k = nums.length - 1; + while (j < k) { + const currentSum = nums[j] + nums[k]; + + if (target < currentSum) { + k--; + } else if (target > currentSum) { + j++; + } else { + result.push([nums[i], nums[j], nums[k]]); + + j++; + k--; + + while (j < k && nums[j] === nums[j - 1]) j++; + while (j < k && nums[k] === nums[k + 1]) k--; + } + } + } + + return result; +} diff --git a/climbing-stairs/hyer0705.ts b/climbing-stairs/hyer0705.ts new file mode 100644 index 000000000..2941d1a63 --- /dev/null +++ b/climbing-stairs/hyer0705.ts @@ -0,0 +1,11 @@ +function climbStairs(n: number): number { + const dp: number[] = Array.from({ length: n + 1 }, () => 0); + dp[0] = 1; + dp[1] = 1; + + for (let i = 2; i < n + 1; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; +} diff --git a/product-of-array-except-self/hyer0705.ts b/product-of-array-except-self/hyer0705.ts new file mode 100644 index 000000000..fc23ea288 --- /dev/null +++ b/product-of-array-except-self/hyer0705.ts @@ -0,0 +1,16 @@ +function productExceptSelf(nums: number[]): number[] { + const n = nums.length; + const result = Array(n).fill(1); + + for (let i = 1; i < n; i++) { + result[i] = nums[i - 1] * result[i - 1]; + } + + let suffixProduct = 1; + for (let i = n - 1; i >= 0; i--) { + result[i] = result[i] * suffixProduct; + suffixProduct *= nums[i]; + } + + return result; +} diff --git a/valid-anagram/hyer0705.ts b/valid-anagram/hyer0705.ts new file mode 100644 index 000000000..087f041b9 --- /dev/null +++ b/valid-anagram/hyer0705.ts @@ -0,0 +1,20 @@ +function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) return false; + + const sCountMap = new Map(); + const tCountMap = new Map(); + + for (let i = 0; i < s.length; i++) { + const currentS = s[i]; + const currentT = t[i]; + + sCountMap.set(currentS, (sCountMap.get(currentS) || 0) + 1); + tCountMap.set(currentT, (tCountMap.get(currentT) || 0) + 1); + } + + for (const [k, v] of sCountMap) { + if (!tCountMap.has(k) || tCountMap.get(k) !== v) return false; + } + + return true; +} diff --git a/validate-binary-search-tree/hyer0705.ts b/validate-binary-search-tree/hyer0705.ts new file mode 100644 index 000000000..a84b33394 --- /dev/null +++ b/validate-binary-search-tree/hyer0705.ts @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isValidBST(root: TreeNode | null): boolean { + const queue: [TreeNode, number, number][] = [[root, -Infinity, +Infinity]]; + + while (queue.length > 0) { + const [currentNode, minVal, maxVal] = queue.shift(); + + if (minVal >= currentNode.val || currentNode.val >= maxVal) return false; + + if (currentNode.left) { + queue.push([currentNode.left, minVal, currentNode.val]); + } + if (currentNode.right) { + queue.push([currentNode.right, currentNode.val, maxVal]); + } + } + + return true; +}