-
-
Notifications
You must be signed in to change notification settings - Fork 207
[hsskey] WEEK 1 solutions #1168
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
return nums.length !== new Set([...nums]).size | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function(nums) { | ||
if (nums.length === 0) return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 당연히 length는 0이 아니겠지하고 조건을 안보고 진행했었는데 이부분 까지 꼼꼼하게 생각하신점이 좋습니다👍 |
||
if (nums.length === 1) return nums[0]; | ||
|
||
const 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(nums[i] + dp[i - 2], dp[i - 1]); | ||
} | ||
|
||
return dp[nums.length - 1]; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
발상은 비슷한데 조금 다른 제 방식은 num에서 while을 +1로도 가고, -1 로도 가게했습니다. 그리고 각각 반복할 때, numSet에서 currentNum을 지워주구요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 관점에서 생각못해본 부분인데 피드백 감사합니다. 말씀해주신대로 while문에서 +1이나 -1로도 가게할 수도 있군요👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
const numSet = new Set(nums); | ||
let maxLength = 0; | ||
|
||
for (const num of nums) { | ||
if (!numSet.has(num - 1)) { | ||
let currentNum = num; | ||
let currentLength = 1; | ||
|
||
while (numSet.has(currentNum + 1)) { | ||
currentNum++; | ||
currentLength++; | ||
} | ||
maxLength = Math.max(maxLength, currentLength); | ||
} | ||
} | ||
|
||
return maxLength; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
const map = new Map() | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if(map.has(nums[i])) { | ||
const prevVal = map.get(nums[i]) | ||
map.set(nums[i], prevVal + 1) | ||
} else { | ||
map.set(nums[i], 1) | ||
} | ||
} | ||
Comment on lines
+9
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (개인적인 생각입니다) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 피드백 감사합니다 🙇♂️ 다만 저는 반복문 처리에서의 미세한 성능 차이를 고려해서 for 루프를 선호하는 편입니다. (주로 실무에서도) 물론 이번 문제에선 실제 성능 차이는 거의 없겠지만, 개인적으로는 성능 상 이점을 조금이라도 취할 수 있는 구조를 습관화하는 쪽을 택하고 있습니다. 다시 한 번 스타일 일관성에 대한 피드백 감사드려요. 더 고민해볼 수 있는 좋은 계기가 됐습니다! |
||
|
||
const result = [...map.entries()].sort((a, b) => b[1] - a[1]).slice(0, k).map((item) => { | ||
return item[0] | ||
}) | ||
return result | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function(nums, target) { | ||
const map = new Map() | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
const needNum = target - nums[i] | ||
if(map.has(needNum)) { | ||
return [i, map.get(needNum)] | ||
} | ||
|
||
map.set(nums[i], i) | ||
} | ||
}; |
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.
여기는 제가 아직 안풀어서 우선 approve 하고 추후 확인하겠습니다.