-
-
Notifications
You must be signed in to change notification settings - Fork 195
[choidabom] WEEK 01 solutions #1176
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
6 commits
Select commit
Hold shift + click to select a range
4df0570
feat: contains-duplicate
choidabom f9b368d
feat: two-sum
choidabom 00d02f2
feat: top-k-frequent-elements
choidabom bd8eb04
fix: lint
choidabom 8414df7
feat: house-robber
choidabom bff73b3
feat: longest-consecutive-sequence
choidabom 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,24 @@ | ||
// https://leetcode.com/problems/contains-duplicate/description/ | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function containsDuplicate(nums: number[]): boolean { | ||
const set = new Set(nums); | ||
return set.size !== nums.length; | ||
} | ||
|
||
function containsDuplicate(nums: number[]): boolean { | ||
const set = new Set(); | ||
|
||
for (const num of nums) { | ||
if (set.has(num)) { | ||
return true; | ||
} | ||
set.add(num); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); |
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 @@ | ||
// https://leetcode.com/problems/house-robber/ | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function rob(nums: number[]): number { | ||
if (nums.length === 1) return nums[0]; | ||
if (nums.length === 2) return Math.max(nums[0], nums[1]); | ||
|
||
const dp: number[] = []; | ||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (let i = 2; i < nums.length; i++) { | ||
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
} | ||
|
||
return dp[nums.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,25 @@ | ||
// https://leetcode.com/problems/longest-consecutive-sequence/ | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function longestConsecutive(nums: number[]): number { | ||
const numSet = new Set(nums); | ||
let maxLen = 0; | ||
|
||
for (const num of numSet) { | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let length = 1; | ||
|
||
while (numSet.has(currentNum + 1)) { | ||
currentNum += 1; | ||
length += 1; | ||
} | ||
|
||
maxLen = Math.max(maxLen, length); | ||
} | ||
} | ||
|
||
return maxLen; | ||
} |
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 @@ | ||
// https://leetcode.com/problems/top-k-frequent-elements/ | ||
|
||
// TC: O(nlogn) | ||
// SC: O(n) | ||
|
||
function topKFrequent(nums: number[], k: number): number[] { | ||
const map = new Map(); | ||
|
||
for (const num of nums) { | ||
if (map.has(num)) { | ||
map.set(num, map.get(num) + 1); | ||
} else { | ||
map.set(num, 1); | ||
} | ||
} | ||
|
||
const sortedMap = [...map].sort((a, b) => b[1] - a[1]); | ||
return sortedMap.splice(0, k).map((item) => item[0]); | ||
} |
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,36 @@ | ||
// https://leetcode.com/problems/two-sum/ | ||
|
||
// TC: O(n^2) | ||
// SC: O(1) | ||
|
||
function twoSum(nums: number[], target: number): number[] { | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] === target) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function twoSum(nums: number[], target: number): number[] { | ||
const map = new Map(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const num = nums[i]; | ||
const diff = target - num; | ||
|
||
if (map.has(diff)) { | ||
return [i, map.get(diff)]; | ||
} else { | ||
map.set(num, i); | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
console.log(twoSum([2, 7, 11, 15], 9)); |
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.
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.
시간복잡도를 줄이기 위해 다양한 풀이로 시도해보는건 정말 좋은 것 같습니다!