-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyer0705] WEEK 01 solutions #1689
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
202c175
contains duplicate solution
hyer0705 668c07b
two sum solution
hyer0705 942bfa7
top k frequent elements solution
hyer0705 091c502
longest consecutive sequence solution
hyer0705 cbb7de6
house robber solution
hyer0705 34ab358
house robber solution
hyer0705 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,3 @@ | ||
function containsDuplicate(nums: number[]): boolean { | ||
return new Set<number>(nums).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,35 @@ | ||
// 0ms | ||
function rob(nums: number[]): number { | ||
const n = nums.length; | ||
|
||
if (n === 1) return nums[0]; | ||
if (n === 2) return Math.max(nums[0], nums[1]); | ||
|
||
const dp: number[] = Array(n).fill(0); | ||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (let i = 2; i < n; i++) { | ||
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); | ||
} | ||
|
||
return dp[n - 1]; | ||
} | ||
|
||
// 1ms | ||
/* | ||
function rob(nums: number[]): number { | ||
const n = nums.length; | ||
|
||
const dp: number[][] = Array.from({ length: n }, () => Array(2).fill(0)); | ||
|
||
dp[0][1] = nums[0]; | ||
|
||
for (let i = 1; i < n; i++) { | ||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); | ||
dp[i][1] = dp[i - 1][0] + nums[i]; | ||
} | ||
|
||
return Math.max(...dp[n - 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,22 @@ | ||
function longestConsecutive(nums: number[]): number { | ||
if (nums.length === 0) return 0; | ||
|
||
const numSet = new Set<number>(nums); | ||
let longest = 0; | ||
|
||
for (const num of numSet) { | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let sequenceLength = 1; | ||
|
||
while (numSet.has(currentNum + 1)) { | ||
currentNum++; | ||
sequenceLength++; | ||
} | ||
|
||
longest = Math.max(longest, sequenceLength); | ||
} | ||
} | ||
|
||
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,25 @@ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
nums.sort((a, b) => a - b); | ||
|
||
const countMap = new Map<number, number>(); | ||
|
||
let pointer = 0; | ||
while (pointer < nums.length) { | ||
const currentNumber = nums[pointer]; | ||
let count = 0; | ||
|
||
while (nums[pointer] === currentNumber) { | ||
pointer++; | ||
count++; | ||
} | ||
|
||
countMap.set(currentNumber, count); | ||
} | ||
|
||
const result = Array.from(countMap) | ||
.sort((a, b) => b[1] - a[1]) | ||
.slice(0, k) | ||
.map((value) => value[0]); | ||
|
||
return result; | ||
} |
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 @@ | ||
function twoSum(nums: number[], target: number): number[] { | ||
const numsLen = nums.length; | ||
const result: number[] = []; | ||
|
||
for (let i = 0; i < numsLen - 1; i++) { | ||
for (let j = i + 1; j < numsLen; j++) { | ||
if (nums[i] + nums[j] === target) { | ||
result.push(i); | ||
result.push(j); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
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.
오 이런 한 줄 짜리 풀이도 있는데 직관적으로 바로 이해가 갑니다!