-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Kyojin-Hwang] WEEK 01 solutions #1682
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,22 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function (nums) { | ||
const map = new Map(); | ||
|
||
for (let num of nums) { | ||
map.set(num, (map.get(num) || 0) + 1); | ||
} | ||
|
||
for (let [_, count] of map) { | ||
if (count > 1) return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
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.
Set 자료구조를 사용한다면 중복을 제거하고 메모리, 속도 측면에서 좋다고 생각합니다. 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. 좋은 의견 감사합니다! |
||
// set 방식으로도 대체가능 | ||
// var containsDuplicate = function(nums) { | ||
// return new Set(nums).size !== nums.length; | ||
// }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function (nums, k) { | ||
const map = new Map(); | ||
const result = []; | ||
|
||
for (let num of nums) { | ||
map.set(num, (map.get(num) || 0) + 1); | ||
} | ||
|
||
const sorted = [...map.entries()] | ||
.sort((a, b) => b[1] - a[1]) // 빈도 기준으로 정렬 | ||
.map((entry) => entry[0]); // 숫자만 추출 | ||
|
||
for (let i = 0; i < k; i++) { | ||
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. return sorted.slice(0, k); slice를 활용하면 바로 반환도 가능합니다. 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. 오 그런 방법이 있었네용!! |
||
result.push(sorted[i]); | ||
} | ||
|
||
return result; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @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 complement = target - nums[i]; | ||
|
||
if (map.has(complement)) { | ||
return [map.get(complement), i]; | ||
} | ||
|
||
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.
js에서 Map 활용을 잘하시고 계시네요 👍🏻
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.
감사합니댱!!