-
-
Notifications
You must be signed in to change notification settings - Fork 249
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.