-
-
Notifications
You must be signed in to change notification settings - Fork 195
[krokerdile] WEEK 01 Solutions #1150
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
9 commits
Select commit
Hold shift + click to select a range
75bcdb5
contains-duplicate solution
krokerdile 27960ef
commit-duplicate 개행문자 추가
krokerdile 84d172b
직전 PR을 참고해서 contains-duplicate 코드 수정
krokerdile 67cd52e
two-sum solution
krokerdile af4419f
two-sum 개행 문자 추가
krokerdile 75af223
top-k-frequent-elements solution
krokerdile b8d9436
top-k-frequent-elements 개행 문자 추가
krokerdile 45241aa
longest-consecutive-seuence solution
krokerdile 985dc96
house-rubber solution
krokerdile 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,48 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
|
||
/** | ||
* 1차 풀이 | ||
* - Map을 사용하여 각 숫자의 개수를 세는 방법 | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
let dict = new Map(); | ||
|
||
nums.forEach((num)=>{ | ||
if(dict.has(num)){ | ||
dict.set(num, dict.get(num)+1); | ||
}else{ | ||
dict.set(num, 1); | ||
} | ||
}) | ||
|
||
for (const num of nums) { | ||
if(dict.get(num) >= 2){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* 2차풀이 | ||
* - Map을 사용하여 각 숫자의 개수를 세는 방법 | ||
* - forEach를 사용하지 않고 for of문을 사용하여 반복문을 돌리는 방법 | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
let dict = new Map(); | ||
|
||
for (const num of nums) { | ||
if(dict.has(num)){ | ||
dict.set(num, dict.get(num)+1); | ||
}else{ | ||
dict.set(num, 1); | ||
} | ||
if(dict.get(num) >= 2){ | ||
return true; | ||
} | ||
} | ||
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,18 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function(nums) { | ||
if (nums.length === 0) return 0; | ||
if (nums.length === 1) return nums[0]; | ||
|
||
let dp = new Array(nums.length); | ||
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], nums[i] + dp[i - 2]); | ||
} | ||
|
||
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,28 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
if (nums.length === 0) return 0; | ||
|
||
const numSet = new Set(nums); | ||
let maxLength = 0; | ||
|
||
for (let num of numSet) { | ||
// 연속 수열의 시작점인지 확인 | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let currentLength = 1; | ||
|
||
// 연속된 숫자 있는 동안 증가 | ||
while (numSet.has(currentNum + 1)) { | ||
currentNum += 1; | ||
currentLength += 1; | ||
} | ||
|
||
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,26 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
let dict = new Map(); | ||
let temp = Array.from(new Set([...nums])); | ||
for(const num of nums){ | ||
if(dict.has(num)){ | ||
dict.set(num, dict.get(num)+1); | ||
}else{ | ||
dict.set(num,1); | ||
} | ||
} | ||
|
||
temp = temp.sort((a,b)=>{ | ||
let aCount = dict.get(a); | ||
let bCount = dict.get(b); | ||
|
||
return bCount - aCount; | ||
}) | ||
|
||
let slice = temp.slice(0,k); | ||
return slice; | ||
}; |
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 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function(nums, target) { | ||
let dict = new Map(); | ||
|
||
for(const [index, num] of nums.entries()){ | ||
if(dict.has(target-num) && dict.get(target-num) != index){ | ||
return [dict.get(target-num),index]; | ||
} | ||
dict.set(num, index); | ||
} | ||
}; |
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.
dict.set(num, (dict.get(num) || 0) + 1);
이렇게 줄여서 써보시면 어떠실까요?
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.
좋은 코멘트 감사합니다. 위의 코멘트도 고려해서 한번 해보겠습니다!