From 196aae928a7cefaa972e011d7ca6912902001dfe Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sun, 30 Mar 2025 10:45:52 +0900 Subject: [PATCH 1/7] solve: Week 01 Two Sum --- two-sum/eunice-hong.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 two-sum/eunice-hong.ts diff --git a/two-sum/eunice-hong.ts b/two-sum/eunice-hong.ts new file mode 100644 index 000000000..1a8542811 --- /dev/null +++ b/two-sum/eunice-hong.ts @@ -0,0 +1,24 @@ +/** + * Finds two numbers in the array that add up to the target value. + * Uses a hash map to store previously seen numbers for O(n) time complexity. + * + * @param nums - An array of integers. + * @param target - The target sum. + * @returns A tuple containing the indices of the two numbers. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + */ +function twoSum(nums: number[], target: number): number[] { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (map.has(complement)) { + return [map.get(complement)!, i]; + } + map.set(nums[i], i); + } + + throw new Error("No solution found"); +} \ No newline at end of file From 19c4a187be67aa84f0a98c5f9e83ca68821dcd4d Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sun, 30 Mar 2025 10:54:39 +0900 Subject: [PATCH 2/7] chore: add empty line at the end of the solution file --- two-sum/eunice-hong.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two-sum/eunice-hong.ts b/two-sum/eunice-hong.ts index 1a8542811..e91d7c1c0 100644 --- a/two-sum/eunice-hong.ts +++ b/two-sum/eunice-hong.ts @@ -21,4 +21,4 @@ function twoSum(nums: number[], target: number): number[] { } throw new Error("No solution found"); -} \ No newline at end of file +} From 3bd81ece4585a5bc612940fe198b9c718c2baba5 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Thu, 3 Apr 2025 21:07:48 +0900 Subject: [PATCH 3/7] solve: Week 01 contains duplicate --- contains-duplicate/eunice-hong.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 contains-duplicate/eunice-hong.ts diff --git a/contains-duplicate/eunice-hong.ts b/contains-duplicate/eunice-hong.ts new file mode 100644 index 000000000..7b1964706 --- /dev/null +++ b/contains-duplicate/eunice-hong.ts @@ -0,0 +1,15 @@ + +/** + * Determines if the array contains any duplicate values. + * Uses a Set to track seen numbers for O(n) time complexity. + * + * @param nums - An array of integers. + * @returns `true` if there are duplicates, `false` otherwise. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + */ +function containsDuplicate(nums: number[]): boolean { + let numSet = new Set(nums); + return numSet.size != nums.length; +}; \ No newline at end of file From d3774cd8356dce59d73b0cbc860a8c6364403935 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Thu, 3 Apr 2025 22:14:35 +0900 Subject: [PATCH 4/7] solve: Week -1 top k frequent elements --- top-k-frequent-elements/eunice-hong.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 top-k-frequent-elements/eunice-hong.ts diff --git a/top-k-frequent-elements/eunice-hong.ts b/top-k-frequent-elements/eunice-hong.ts new file mode 100644 index 000000000..703068917 --- /dev/null +++ b/top-k-frequent-elements/eunice-hong.ts @@ -0,0 +1,23 @@ + +/** + * Finds the k most frequent elements in an array. + * Uses a map to count occurrences and then sorts by frequency. + * + * @param nums - An array of integers. + * @param k - The number of most frequent elements to return. + * @returns An array of the k most frequent elements. + * + * Time Complexity: O(n log n) + * Space Complexity: O(n) + */ +function topKFrequent(nums: number[], k: number): number[] { + let numMap = new Map(); + for (let i = 0; i < nums.length; i++) { + numMap.set(nums[i], (numMap.get(nums[i]) ?? 0) + 1); + } + + return Array.from(numMap.entries()) + .sort((a, b) => b[1] - a[1]) + .slice(0, k) + .map(([num, _]) => num); +}; \ No newline at end of file From 257723336bdea1d1019246399d00282058aac391 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Thu, 3 Apr 2025 22:58:50 +0900 Subject: [PATCH 5/7] fix: add missing newline at the end of the solution files --- contains-duplicate/eunice-hong.ts | 2 +- top-k-frequent-elements/eunice-hong.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/eunice-hong.ts b/contains-duplicate/eunice-hong.ts index 7b1964706..34bcc2f5a 100644 --- a/contains-duplicate/eunice-hong.ts +++ b/contains-duplicate/eunice-hong.ts @@ -12,4 +12,4 @@ function containsDuplicate(nums: number[]): boolean { let numSet = new Set(nums); return numSet.size != nums.length; -}; \ No newline at end of file +}; diff --git a/top-k-frequent-elements/eunice-hong.ts b/top-k-frequent-elements/eunice-hong.ts index 703068917..7d0fdcfad 100644 --- a/top-k-frequent-elements/eunice-hong.ts +++ b/top-k-frequent-elements/eunice-hong.ts @@ -20,4 +20,4 @@ function topKFrequent(nums: number[], k: number): number[] { .sort((a, b) => b[1] - a[1]) .slice(0, k) .map(([num, _]) => num); -}; \ No newline at end of file +}; From dc07507da75f70545c90c8340fdbbe99055aaeb6 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sat, 5 Apr 2025 22:40:46 +0900 Subject: [PATCH 6/7] solve: Week 01 house robber --- house-robber/eunice-hong.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 house-robber/eunice-hong.ts diff --git a/house-robber/eunice-hong.ts b/house-robber/eunice-hong.ts new file mode 100644 index 000000000..b92281b6d --- /dev/null +++ b/house-robber/eunice-hong.ts @@ -0,0 +1,16 @@ +/** + * Finds the maximum amount of money that can be robbed without robbing two adjacent houses. + * Uses dynamic programming to track the best outcome at each step. + * + * @param {number[]} nums + * @return {number} + */ +function rob(nums: number[]): number { + const dp = new Array(nums.length + 1); + dp[0] = 0; + dp[1] = nums[0]; + for (let i = 2; i < dp.length; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]); + } + return dp[dp.length - 1]; +}; From 3cc08ecb71c4088eaddfb9f898aa28c0718f441c Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Sat, 5 Apr 2025 22:44:59 +0900 Subject: [PATCH 7/7] solve: Week 01 longest consecutive sequence --- longest-consecutive-sequence/eunice-hong.ts | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-consecutive-sequence/eunice-hong.ts diff --git a/longest-consecutive-sequence/eunice-hong.ts b/longest-consecutive-sequence/eunice-hong.ts new file mode 100644 index 000000000..b466798c2 --- /dev/null +++ b/longest-consecutive-sequence/eunice-hong.ts @@ -0,0 +1,26 @@ +/** + * Finds the length of the longest consecutive elements sequence. + * Eliminates duplicates using a Set and only starts counting when the current number is the beginning of a sequence. + * + * @param nums - An array of integers. + * @returns The length of the longest consecutive sequence. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + */ +function longestConsecutive(nums: number[]): number { + let longest = 0; + const numSet = new Set(nums); + + for (const num of numSet) { + if (!numSet.has(num - 1)) { + let length = 1; + while (numSet.has(num + length)) { + length++; + } + longest = Math.max(length, longest); + } + } + + return longest; +}