-
-
Notifications
You must be signed in to change notification settings - Fork 195
[b41-41] WEEK 01 solutions #1161
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
26aa877
0af9854
e15b852
88cb348
efcbc1a
88858ba
c4e76c6
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,15 @@ | ||
// nums에 중복이 있는 지 확인하는 것 | ||
// array써서 시간 통과 못했다가 Set 객체로 변경해서 통과 | ||
function containsDuplicate(nums: number[]): boolean { | ||
const numSet = new Set(); | ||
|
||
for(let num of nums) { | ||
if(numSet.has(num)) { | ||
return true; | ||
} else { | ||
numSet.add(num); | ||
} | ||
} | ||
|
||
return false; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function longestConsecutive(nums: number[]): number { | ||
const sortedNums = [...new Set(nums.sort((a, b) => a - b))]; | ||
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. 자바스크립트의 sort 함수는 O(n log n)의 시간복잡도를 가지는데, 문제에서는 'You must write an algorithm that runs in O(n) time'라고 명시되어 있었어요! (근데 저도 n log n으로 풀어봤었는데 통과는 되더라는 .... ) Set을 활용한 O(n) 접근법을 고려해보시면 좋을 것 같습니다. 연속 수열의 시작점을 찾는 방식은 정렬 없이도 문제를 해결할 수 있더라구요! 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과 Set.has를 사용하니 정말 쉽게 풀리네요! 설명 감사합니다!! |
||
const numSet = new Set(); | ||
|
||
let dummy: number[] = []; | ||
|
||
for (let i: number = 0; i < sortedNums.length; i++) { | ||
const isConsecutiveSequence = sortedNums[i + 1] - sortedNums[i] === 1; | ||
const num = sortedNums[i]; | ||
|
||
if(!isConsecutiveSequence) { | ||
dummy.push(num) | ||
numSet.add(dummy.length); | ||
dummy = []; | ||
} else { | ||
dummy.push(num); | ||
} | ||
} | ||
|
||
numSet.add(dummy.length); | ||
|
||
const result = [...numSet].sort((a, b) => Number(b) - Number(a)) as number[]; | ||
return result[0] || 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const numMap = new Map(); | ||
|
||
for (let num of nums) { | ||
if(!numMap.has(num)) { | ||
numMap.set(num, 1); | ||
} else { | ||
const count = Number(numMap.get(num)) || 0; | ||
numMap.set(num, count + 1); | ||
} | ||
} | ||
|
||
const result = [...numMap].sort((a, b) => b[1] - a[1]).map((num) => num[0]).slice(0, k); | ||
|
||
|
||
return result; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 1차 시도 | ||
// function twoSum(nums: number[], target: number): number[] { | ||
// for(const index in nums) { | ||
// for(const index2 in nums) { | ||
// if (index !== index2) { | ||
// if(target === nums[index] + nums[index2]) { | ||
// return [Number(index), Number(index2)] | ||
// } | ||
// } | ||
// } | ||
// } | ||
// }; | ||
|
||
// 2차 시도 | ||
function twoSum(nums: number[], target: number): number[] { | ||
for (const index in nums) { | ||
for (let index2 = Number(index) + 1; index2 < nums.length; index2++) { | ||
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. 현재 구현하신 방식은 이중 반복문을 사용하여 O(n²) 시간 복잡도를 가지는 것으로 보여요! Map 자료구조를 활용하면 한 번의 순회만으로 O(n) 시간 복잡도로 문제를 해결할 수 있는데, 각 숫자를 확인하면서 '현재 값 + x = target'이 되는 x를 찾는 대신, 'target - 현재 값 = x'를 Map에 저장해두는 방식도 좋을 것 같습니다! 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. 말씀하신 방법으로 구현해 보니 더 깔끔하고, 감사합니다! |
||
if(target === nums[index] + nums[index2]) { | ||
return [Number(index), Number(index2)] | ||
} | ||
} | ||
|
||
} | ||
|
||
return []; | ||
}; |
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.
이렇게 반복문으로 구현하셔도 충분히 잘 작동하지만, 조금 더 간단한 방법을 제안드려요!
초반에 nums 배열을 바로 Set으로 변환하면 중복 숫자가 자동으로 제거되니 원본 배열 길이와 Set의 크기가 다르다면 중복 숫자가 있었다는 의미가 됩니다!
요 방식을 사용하면 코드가 더 간결해질 수 있을 것 같아요! 😊
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.
Set 객체 사용하면 중복 제거가 되는데,
for문을 다시 돌렸네요.. 🫠
감사합니다!!