From cbbad7b145b91bde96611e7464a344dc7d639907 Mon Sep 17 00:00:00 2001 From: casentino Date: Thu, 20 Nov 2025 17:23:32 +0900 Subject: [PATCH 1/5] Valid Anagram --- valid-anagram/casentino.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-anagram/casentino.ts diff --git a/valid-anagram/casentino.ts b/valid-anagram/casentino.ts new file mode 100644 index 0000000000..122dc3873e --- /dev/null +++ b/valid-anagram/casentino.ts @@ -0,0 +1,16 @@ +function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) { + return false; + } + const count = new Array(26).fill(0); + for (let i = 0; i < s.length; i++) { + count[s.charCodeAt(i) - 97]++; + count[t.charCodeAt(i) - 97]--; + } + for (let c of count) { + if (c !== 0) { + return false; + } + } + return true; +} From 5e2178fe718db8826a54369c26643c5ee0321618 Mon Sep 17 00:00:00 2001 From: casentino Date: Thu, 20 Nov 2025 22:34:54 +0900 Subject: [PATCH 2/5] Climbing Stairs --- climbing-stairs/casentino.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 climbing-stairs/casentino.ts diff --git a/climbing-stairs/casentino.ts b/climbing-stairs/casentino.ts new file mode 100644 index 0000000000..e4698d7310 --- /dev/null +++ b/climbing-stairs/casentino.ts @@ -0,0 +1,17 @@ +function climbStairs(n: number): number { + const memo = new Array(n + 1).fill(-1); + function climb(stair: number) { + if (stair === 0) { + return 1; + } + if (stair < 0) { + return 0; + } + if (memo[stair] !== -1) { + return memo[stair]; + } + memo[stair] = climb(stair - 1) + climb(stair - 2); + return memo[stair]; + } + return climb(n); +} From b83f655f5005ba77d5e7c865eee496c90cb788ad Mon Sep 17 00:00:00 2001 From: casentino Date: Thu, 20 Nov 2025 22:44:25 +0900 Subject: [PATCH 3/5] Product of Array Except Self --- product-of-array-except-self/casentino.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 product-of-array-except-self/casentino.ts diff --git a/product-of-array-except-self/casentino.ts b/product-of-array-except-self/casentino.ts new file mode 100644 index 0000000000..373c89ab68 --- /dev/null +++ b/product-of-array-except-self/casentino.ts @@ -0,0 +1,14 @@ +function productExceptSelf(nums: number[]): number[] { + const len = nums.length; + const result = new Array(len).fill(1); + let pre = 1; + let post = 1; + for (let i = 0; i < len; i++) { + result[i] *= pre; + pre *= nums[i]; + + result[len - i - 1] *= post; + post *= nums[len - i - 1]; + } + return result; +} From ba7392716d0a6e57ae4bd3e1637753c31fea5879 Mon Sep 17 00:00:00 2001 From: casentino Date: Thu, 20 Nov 2025 23:53:18 +0900 Subject: [PATCH 4/5] 3Sum --- 3sum/casentino.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 3sum/casentino.ts diff --git a/3sum/casentino.ts b/3sum/casentino.ts new file mode 100644 index 0000000000..d643f574db --- /dev/null +++ b/3sum/casentino.ts @@ -0,0 +1,34 @@ +function threeSum(nums: number[]): number[][] { + const sortedNums = nums.sort((a, b) => a - b); + const results: number[][] = []; + for (let i = 0; i < sortedNums.length - 2; i++) { + const current = sortedNums[i]; + if (current > 0) { + return results; + } + if (i > 0 && current === sortedNums[i - 1]) { + continue; + } + let left = i + 1; + let right = sortedNums.length - 1; + while (left < right) { + const sum = current + sortedNums[left] + sortedNums[right]; + if (sum > 0) { + right -= 1; + } else if (sum < 0) { + left += 1; + } else { + results.push([current, sortedNums[left], sortedNums[right]]); + left += 1; + right -= 1; + while (left < right && sortedNums[left] === sortedNums[left - 1]) { + left += 1; + } + while (left < right && sortedNums[right] === sortedNums[right + 1]) { + right -= 1; + } + } + } + } + return results; +} From 08497f1608bd49024953f5ac775a523a8751f110 Mon Sep 17 00:00:00 2001 From: casentino Date: Fri, 21 Nov 2025 01:16:42 +0900 Subject: [PATCH 5/5] Validate Binary Search Tree --- validate-binary-search-tree/casentino.ts | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 validate-binary-search-tree/casentino.ts diff --git a/validate-binary-search-tree/casentino.ts b/validate-binary-search-tree/casentino.ts new file mode 100644 index 0000000000..9615f585ce --- /dev/null +++ b/validate-binary-search-tree/casentino.ts @@ -0,0 +1,39 @@ +/** + * 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) + * } + * } + */ +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 { + function recursiveSearch(node: TreeNode | null, min: number | null, max: number | null) { + if (!node) { + return true; + } + if (min !== null && node.val <= min) { + return false; + } + if (max !== null && node.val >= max) { + return false; + } + return recursiveSearch(node.left, min, node.val) && recursiveSearch(node.right, node.val, max); + } + return recursiveSearch(root, null, null); +}