-
-
Notifications
You must be signed in to change notification settings - Fork 248
[Yg-cho] WEEK 01 Solutions #1707
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
f8350fd
29c9a3b
c102fc8
28aca7d
7c68cbd
6aaeaa3
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,11 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
return new Set(nums).size !== nums.length; | ||
}; | ||
|
||
//console.log(containsDuplicate([1, 2, 3, 1])); // true | ||
// console.log(containsDuplicate([1, 2, 3, 4])); // false | ||
// console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums) { | ||
if(nums.length === 0) return 0; | ||
|
||
const numSet = new Set(nums); | ||
let longest = 0; | ||
|
||
for (const num of numSet) { | ||
if(!numSet.has(num-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. 자바스크립트는 익숙치 않아서 제가 제대로 이해한진 잘 모르겠습니다 저도 처음엔 중첩루프로 풀었는데요, dynamic programming을 이용해서 nums와 동일한 크기의 배열을 만들고 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. 안녕하세요. 리뷰 감사합니다~ |
||
let currentNum = num; | ||
let currentLength = 1; | ||
|
||
while(numSet.has(currentNum+1)) { | ||
currentNum++; | ||
currentLength++; | ||
} | ||
|
||
longest = Math.max(longest, currentLength) | ||
} | ||
} | ||
return longest; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function(nums, k) { | ||
//HashMap 선언 | ||
const counter = new Map() | ||
|
||
//HashMap에 빈도를 value로 저장 | ||
for(const num of nums){ | ||
counter.set(num,(counter.get(num)|| 0) +1); | ||
} | ||
|
||
//keys를 가져와 정렬 후, k만큼 -slice 리턴 | ||
return [...counter.keys()] | ||
.sort((a,b) => counter.get(a) - counter.get(b)) | ||
.slice(-k) | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function(nums, target) { | ||
for(var i = 0; i < nums.length; i++){ | ||
let getNum = target - nums[i]; | ||
let foundIndex = nums.indexOf(getNum); | ||
|
||
// foundIndex가 존재하고(-1이 아니고), 자기 자신이 아닌 경우 | ||
if(foundIndex !== -1 && foundIndex !== i) { | ||
return [i, foundIndex]; | ||
} | ||
} | ||
}; | ||
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1] | ||
console.log(twoSum([3, 2, 4], 6)); // [1, 2] | ||
console.log(twoSum([3, 3], 6)); // [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.
전 개수를 세서 2개이상인지 판단했는데 set을 이용한건 되게 좋은 아이디어네요