- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 195
[seungseung88] WEEK 01 solutions #1136
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
Conversation
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.
공간복잡도랑 시간복잡도까지 다 파악해서 기록해 두시는게 대단하신 것 같아요!!
첫 주 마지막 문제까지 잘 하시길 응원합니다!! 고생하셨어요~
two-sum/seungseung88.js
Outdated
const twoSum = (nums, target) => { | ||
// 배열을 한 번 순회 | ||
// {값: 인덱스} 형태로 저장 | ||
const indicies = {}; | ||
|
||
for (let i = 0; i < nums.length; i += 1) { | ||
// target숫자에서 현재 숫자를 뺀 숫자가 존재하는지 확인한다. | ||
const targetIndex = nums.indexOf(target - nums[i]); | ||
// targetIndex가 존재하고(-1이 아님), 현재 숫자와 같은 인덱스 숫자가 아니라면 반환한다. | ||
if (targetIndex !== -1 && i !== targetIndex) { | ||
return [i, targetIndex]; | ||
// 타겟값에서 현재 가리키는 숫자를 뺀 값을 저장 | ||
const complement = target - nums[i]; | ||
|
||
// complement가 indicies안에 존재하면 해당 값을 반환 | ||
if (complement in indicies) { | ||
const j = indicies[complement]; | ||
return [j, i]; | ||
} | ||
// 존재 하지 않으면 값과 인덱스 형태로 저장 ex> { 11: 0, 15: 1, 2: 2 } | ||
indicies[nums[i]] = i; | ||
console.log(indicies); | ||
} | ||
}; |
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.
객체를 이용해서 푸셨네요!
저는 Map을 통해서 관계를 그렸는데 다양한 풀이법이 있는 것 같네요
const answer = []; | ||
|
||
// 상위 k개의 숫자를 answer 배열에 저장 | ||
for (let i = 0; i < k; i += 1) { | ||
answer[i] = Number(sortedCountNums[i][0]); | ||
} |
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.
선언적으로 처리한다면 다음과 같은 방식으로 처리할수도 있을 것 같아요!
const answer = []; | |
// 상위 k개의 숫자를 answer 배열에 저장 | |
for (let i = 0; i < k; i += 1) { | |
answer[i] = Number(sortedCountNums[i][0]); | |
} | |
const answer = Array.from(sortedCountNums.entries()) | |
.sort((a, b) => b[1] - a[1]) | |
.slice(0, k) | |
.map(item => item[0]); |
// 연속되는 배열에서 가장 긴 배열을 저장 | ||
while (set.has(v + cnt)) { | ||
cnt += 1; | ||
longest = longest < cnt ? cnt : longest; |
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.
의도는 충분히 알겠으나 단순히 아래 코드가 가독성이 더 나을수도 있겠다는 생각이 드네요 🤔
if (cnt > longest) {
longest = cnt;
}
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!