From cf40e6ad0345ccf8463e4e8d292ba43260663648 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Thu, 24 Jul 2025 22:18:21 +0900 Subject: [PATCH 1/5] contains duplicate solution --- contains-duplicate/nancyel.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/nancyel.ts diff --git a/contains-duplicate/nancyel.ts b/contains-duplicate/nancyel.ts new file mode 100644 index 000000000..f6f199816 --- /dev/null +++ b/contains-duplicate/nancyel.ts @@ -0,0 +1,14 @@ +/** + * time complexity: O(n) + * space complexity: O(n) + */ +function containsDuplicate(nums: number[]): boolean { + const seen = new Set(); + for (let num of nums) { + if (seen.has(num)) { + return true; + } + seen.add(num); + } + return false; +} From cb574a6ba991e344492e297e002939305fbe940c Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Thu, 24 Jul 2025 22:44:07 +0900 Subject: [PATCH 2/5] two sum solution --- two-sum/nancyel.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 two-sum/nancyel.ts diff --git a/two-sum/nancyel.ts b/two-sum/nancyel.ts new file mode 100644 index 000000000..8ac5cbb2e --- /dev/null +++ b/two-sum/nancyel.ts @@ -0,0 +1,19 @@ +/** + * Time Complexity: O(n) (using a single pass with a hash map) + * (If a nested loop was used, it would be O(n^2)) + */ +function twoSum(nums: number[], target: number): number[] { + const numAndIndex = new Map(); + + for (let i = 0; i < nums.length; i++) { + const diff = target - nums[i]; + + if (numAndIndex.has(diff)) { + return [numAndIndex.get(diff)!, i]; + } + + numAndIndex.set(nums[i], i); + } + + return []; +} From f9ab86240f9240602913f92762cd679d8cc5f6e8 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 25 Jul 2025 20:56:24 +0900 Subject: [PATCH 3/5] top k freq solution --- top-k-frequent-elements/nancyel.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 top-k-frequent-elements/nancyel.ts diff --git a/top-k-frequent-elements/nancyel.ts b/top-k-frequent-elements/nancyel.ts new file mode 100644 index 000000000..5fd093fbd --- /dev/null +++ b/top-k-frequent-elements/nancyel.ts @@ -0,0 +1,21 @@ +/** + * time complexity: O(n + m log m), + * n = length of input array + * m = number of unique elements (m ≤ n) + * space complexity: O(m) + */ + +function topKFrequent(nums: number[], k: number): number[] { + const freqMap = new Map(); + + // Count frequencies using a map: O(n) + for (const num of nums) { + freqMap.set(num, (freqMap.get(num) || 0) + 1); + } + + // Extract the top k elements + return Array.from(freqMap.entries()) // O(m) + .sort(([, a], [, b]) => b - a) // O(m log m) + .slice(0, k) + .map(([num]) => num); // extract the keys only +} From 94f2c102a0035c5ca9ac78a6731ef7e264fe13b0 Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 25 Jul 2025 21:32:31 +0900 Subject: [PATCH 4/5] longest consecutive seq solution --- longest-consecutive-sequence/nancyel.ts | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 longest-consecutive-sequence/nancyel.ts diff --git a/longest-consecutive-sequence/nancyel.ts b/longest-consecutive-sequence/nancyel.ts new file mode 100644 index 000000000..79dc79161 --- /dev/null +++ b/longest-consecutive-sequence/nancyel.ts @@ -0,0 +1,27 @@ +/** + * requirement: return result in linear time (O(n)) + */ +function longestConsecutive(nums: number[]): number { + if (nums.length === 0) return 0; + + const numSet = new Set(nums); // O(1) lookups + let maxLength = 0; + + for (const num of numSet) { + // key: determine the "start" of a consecutive sequence. + // approach: if num - 1 doesn't exist in the set → num is the start of a sequence. + if (!numSet.has(num - 1)) { + let currentNum = num; + let currentLength = 1; + + // count consecutive numbers + while (numSet.has(currentNum + 1)) { + currentNum++; + currentLength++; + } + + maxLength = Math.max(maxLength, currentLength); + } + } + return maxLength; +} From 52742bd7b3c03b69a1b366d20dcce240583ee3bc Mon Sep 17 00:00:00 2001 From: Nancy Lee Date: Fri, 25 Jul 2025 23:54:16 +0900 Subject: [PATCH 5/5] house robber solution --- house-robber/nancyel.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 house-robber/nancyel.ts diff --git a/house-robber/nancyel.ts b/house-robber/nancyel.ts new file mode 100644 index 000000000..16bb0b073 --- /dev/null +++ b/house-robber/nancyel.ts @@ -0,0 +1,22 @@ +/** + * time complexity: O(n) - iterate over a loop + * space complexity: O(n) - dp array + * + * comment: initial naive implementation: simple odd/even alternation, which may return result that is "accidentally correct." + */ +function rob(nums: number[]): number { + // early return + if (nums.length === 0) return 0; + if (nums.length === 1) return nums[0]; + + const dp: number[] = new Array(nums.length); + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + for (let i = 2; i < nums.length; i++) { + // select either 1) current + best from 2 houses ago or 2) skip current, best from previous + dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]); + } + + return dp[nums.length - 1]; // max money from all houses +}