Skip to content

[nancyel] WEEK 01 solutions #1713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contains-duplicate/nancyel.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions house-robber/nancyel.ts
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions longest-consecutive-sequence/nancyel.ts
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions top-k-frequent-elements/nancyel.ts
Original file line number Diff line number Diff line change
@@ -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<number, number>();

// 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
}
19 changes: 19 additions & 0 deletions two-sum/nancyel.ts
Original file line number Diff line number Diff line change
@@ -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<number, number>();

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 [];
}