-
-
Notifications
You must be signed in to change notification settings - Fork 195
[eunice-hong] Week 1 #1126
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
[eunice-hong] Week 1 #1126
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
196aae9
solve: Week 01 Two Sum
eunice-hong 19c4a18
chore: add empty line at the end of the solution file
eunice-hong 3bd81ec
solve: Week 01 contains duplicate
eunice-hong d3774cd
solve: Week -1 top k frequent elements
eunice-hong 2577233
fix: add missing newline at the end of the solution files
eunice-hong dc07507
solve: Week 01 house robber
eunice-hong 3cc08ec
solve: Week 01 longest consecutive sequence
eunice-hong 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,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; | ||
}; |
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,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]; | ||
}; |
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,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; | ||
} |
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,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); | ||
}; |
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,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<number, number>(); | ||
|
||
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"); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eunice-hong 님 안녕하세요!
문제에 대한 설명을 작성해주시고, 코드가 간결해서 보기 쉬웠습니다. 타입스크립트는 익숙하지 않은 언어인데, 저와 비슷한 방식으로 문제를 푸셔서 코드를 따라가기 수월했던 것 같아요. 스터디 첫째주 고생하셨습니다 😊👍