diff --git a/3sum/sooooo-an.ts b/3sum/sooooo-an.ts new file mode 100644 index 000000000..4570b4e9c --- /dev/null +++ b/3sum/sooooo-an.ts @@ -0,0 +1,30 @@ +function threeSum(nums: number[]): number[][] { + nums.sort((a: number, b: number) => a - b); + const results: [number, number, number][] = []; + + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const current = nums[i] + nums[left] + nums[right]; + if (current === 0) { + results.push([nums[i], nums[left], nums[right]]); + + while (left < right && nums[left] === nums[left + 1]) left++; + while (left < right && nums[right] === nums[right - 1]) right--; + + left++; + right--; + } else if (current < 0) { + left++; + } else if (current > 0) { + right--; + } + } + } + + return results; +} diff --git a/climbing-stairs/sooooo-an.ts b/climbing-stairs/sooooo-an.ts new file mode 100644 index 000000000..716520185 --- /dev/null +++ b/climbing-stairs/sooooo-an.ts @@ -0,0 +1,19 @@ +function climbStairs(n: number): number { + type Cache = { [key: number]: number }; + + const cache: Cache = { + 2: 2, + 1: 1, + }; + + const fibonacci = (n: number, cache: Cache) => { + if (n in cache) { + return cache[n]; + } else { + cache[n] = fibonacci(n - 2, cache) + fibonacci(n - 1, cache); + return cache[n]; + } + }; + + return fibonacci(n, cache); +} diff --git a/product-of-array-except-self/sooooo-an.ts b/product-of-array-except-self/sooooo-an.ts new file mode 100644 index 000000000..e3816d178 --- /dev/null +++ b/product-of-array-except-self/sooooo-an.ts @@ -0,0 +1,18 @@ +function productExceptSelf(nums: number[]): number[] { + const results = []; + + let left = 1; + let right = 1; + + for (let i = 0; i < nums.length; i++) { + results[i] = left; + left *= nums[i]; + } + + for (let i = nums.length - 1; i >= 0; i--) { + results[i] *= right; + right *= nums[i]; + } + + return results; +} diff --git a/valid-anagram/sooooo-an.ts b/valid-anagram/sooooo-an.ts new file mode 100644 index 000000000..985be9505 --- /dev/null +++ b/valid-anagram/sooooo-an.ts @@ -0,0 +1,18 @@ +function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) return false; + + const objS: { [key: string]: number } = {}; + + for (const str of s) { + objS[str] = (objS[str] ?? 0) + 1; + } + + for (const str of t) { + if (!objS[str]) { + return false; + } + objS[str]--; + } + + return true; +} diff --git a/validate-binary-search-tree/sooooo-an.ts b/validate-binary-search-tree/sooooo-an.ts new file mode 100644 index 000000000..b0ab1f9f5 --- /dev/null +++ b/validate-binary-search-tree/sooooo-an.ts @@ -0,0 +1,18 @@ +function isValidBST(root: TreeNode | null): boolean { + return validateBSTHelper(root, -Infinity, Infinity); +} + +const validateBSTHelper = ( + root: TreeNode | null, + minValue: number, + maxValue: number +): boolean => { + if (root === null) { + return true; + } + if (root.val <= minValue || root.val >= maxValue) { + return false; + } + const leftIsValid = validateBSTHelper(root.left, minValue, root.val); + return leftIsValid && validateBSTHelper(root.right, root.val, maxValue); +};