From cb7279f03077690c4cf4280432244d5378ca2119 Mon Sep 17 00:00:00 2001 From: Eunice Hong Date: Mon, 7 Apr 2025 15:50:29 +0900 Subject: [PATCH 1/5] solve: Week 02 valid anagram --- valid-anagram/eunice-hong.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 valid-anagram/eunice-hong.ts diff --git a/valid-anagram/eunice-hong.ts b/valid-anagram/eunice-hong.ts new file mode 100644 index 000000000..b49de05e3 --- /dev/null +++ b/valid-anagram/eunice-hong.ts @@ -0,0 +1,22 @@ +function isAnagram(s: string, t: string): boolean { + // if the length of the two strings are not the same, return false + if (s.length !== t.length) return false; + + // create a map of the characters in string s and t + const sMap = new Map(); + const tMap = new Map(); + + // iterate through the strings and add the characters to the maps + for (let i = 0; i < s.length; i++) { + sMap.set(s[i], (sMap.get(s[i]) ?? 0) + 1); + tMap.set(t[i], (tMap.get(t[i]) ?? 0) + 1); + } + + // if the values of the maps are not the same, return false + for (let char of sMap.keys()) { + if (sMap.get(char) !== tMap.get(char)) { + return false; + } + } + return true; +} From 63ec43372eb0124052c25b06646beadebf0ab2ca Mon Sep 17 00:00:00 2001 From: Eunice Hong Date: Mon, 7 Apr 2025 16:37:10 +0900 Subject: [PATCH 2/5] solve: Week 02 climbing stairs --- climbing-stairs/eunice-hong.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 climbing-stairs/eunice-hong.ts diff --git a/climbing-stairs/eunice-hong.ts b/climbing-stairs/eunice-hong.ts new file mode 100644 index 000000000..418cacd4e --- /dev/null +++ b/climbing-stairs/eunice-hong.ts @@ -0,0 +1,22 @@ +function climbStairs(n: number): number { + // memoization to store the results of the subproblems + const memo = new Map(); + + // if n is 1 or 2, return n + memo.set(1, 1); + memo.set(2, 2); + + // recursive function to calculate the number of ways to climb the stairs + function climb(n: number, memo: Map): number { + let result = memo.get(n); + if (result) { + return result; + } else { + result = climb(n - 1, memo) + climb(n - 2, memo); + memo.set(n, result); + return result; + } + } + + return climb(n, memo); +} From 5badd68fcfa37d261e4c6e1c2a994ee5a3b9b715 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sat, 12 Apr 2025 18:23:30 +0900 Subject: [PATCH 3/5] solve: Week 02 product of array except self --- product-of-array-except-self/eunice-hong.ts | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 product-of-array-except-self/eunice-hong.ts diff --git a/product-of-array-except-self/eunice-hong.ts b/product-of-array-except-self/eunice-hong.ts new file mode 100644 index 000000000..814b984a0 --- /dev/null +++ b/product-of-array-except-self/eunice-hong.ts @@ -0,0 +1,37 @@ +function productExceptSelf(nums: number[]): number[] { + // Check if there are zeros in the array + const zeroCount = nums.reduce((acc, num) => { + if (num === 0) { + return acc + 1; + } else { + return acc; + } + }, 0); + + if (zeroCount === 0) { + // If there are no zeros, calculate the product of all numbers + const totalProduct = nums.reduce((acc, num) => num * acc, 1); + return nums.map((num) => { + return totalProduct / num; + }); + } else if (zeroCount === 1) { + // If there is one zero, calculate the product of all numbers except the zero + const totalProduct = nums.reduce((acc, num) => { + if (num === 0) { + return acc; + } else { + return num * acc; + } + }, 1); + return nums.map((num) => { + if (num === 0) { + return totalProduct + } else { + return 0; + } + }); + } else { + // If there are more than one zero, return an array of zeros + return nums.map((_) => 0); + } +}; From 94501882a6cf247246a6e4c426a4b7b2c4a60b79 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sat, 12 Apr 2025 18:43:12 +0900 Subject: [PATCH 4/5] solve: Week 02 3 sum --- 3sum/eunice-hong.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 3sum/eunice-hong.ts diff --git a/3sum/eunice-hong.ts b/3sum/eunice-hong.ts new file mode 100644 index 000000000..e7a603d36 --- /dev/null +++ b/3sum/eunice-hong.ts @@ -0,0 +1,36 @@ +function threeSum(nums: number[]): number[][] { + // Sort the array to make it easier to find triplets that sum to zero + nums.sort((a, b) => a - b); + const result: number[][] = []; + + // Iterate through the array to find triplets that sum to zero + for (let i = 0; i < nums.length - 2; i++) { + // Skip duplicates + if (i > 0 && nums[i] === nums[i - 1]) continue; + + // Use two pointers to find the other two numbers + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const sum = nums[i] + nums[left] + nums[right]; + if (sum === 0) { + // Add the triplet to the result array + result.push([nums[i], nums[left], nums[right]]); + left++; + right--; + // Skip duplicates + while (left < right && nums[left] === nums[left - 1]) left++; + while (left < right && nums[right] === nums[right + 1]) right--; + } else if (sum < 0) { + // Move the left pointer to the right to increase the sum + left++; + } else { + // Move the right pointer to the left to decrease the sum + right--; + } + } + } + + return result; +} From 9c157845c5eab5a41fd222d121fc853b65d2fe36 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sat, 12 Apr 2025 21:06:20 +0900 Subject: [PATCH 5/5] solve: Week 02 validate binary search tree --- validate-binary-search-tree/eunice-hong.ts | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 validate-binary-search-tree/eunice-hong.ts diff --git a/validate-binary-search-tree/eunice-hong.ts b/validate-binary-search-tree/eunice-hong.ts new file mode 100644 index 000000000..3cfb983ad --- /dev/null +++ b/validate-binary-search-tree/eunice-hong.ts @@ -0,0 +1,29 @@ +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 { + // Helper function to check if a tree is a valid binary search tree + function isValidTree(node: TreeNode | null, min: number | null, max: number | null): boolean { + // If the node is null, the tree is valid + if (node === null) return true; + + // If the node's value is less than the minimum or greater than the maximum, the tree is not valid + if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) { + return false; + } + + // Recursively check the left and right subtrees + return isValidTree(node.left, min, node.val) && isValidTree(node.right, node.val, max); + } + + // Check if the tree is a valid binary search tree + return isValidTree(root, null, null); +}