Skip to content

[mmyeon] Week 1 #1144

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

Merged
merged 6 commits into from
Apr 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions two-sum/mmyeon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,30 @@ function twoSum(nums: number[], target: number): number[] {
map.set(nums[i], i);
}
}

/**
*
* 접근 방법 :
* - 현재 숫자가 확인한 숫자들 목록에 있으면 해당 숫자의 인덱스와 현재 인덱스를 바로 리턴
* - 숫자들 목록에 없으면 숫자와 인덱스를 map에 저장
*
* 시간복잡도 : O(n)
* - nums 배열을 1회만 순회하니까 O(n)
*
* 공간복잡도 : O(n)
* - 최악의 경우, nums 배열 크기만큼 map에 저장하니까 O(n)
*/
function twoSum(nums: number[], target: number): number[] {
const seenNumbers = new Map<number, number>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new 이후에 free가 없어서 검색해 봤는데 javascript는 new 이후에 free 가 필요 없군요
새로운 사실을 알았습니다. 감사합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PDKhan
안녕하세요 반갑습니다!
리뷰 남겨주셔서 감사해요.

어떤 언어 사용하시는지 궁금해서 PR 체크해봤는데, 제 다음 PR 작성자가 아니시더라구요.
아마 리뷰어 할당에 착오가 있으셨던 것 같아요.

PDKhan님 PR 이전 PR에 다시 한 번 할당 부탁드립니다~
제 PR 리뷰어는 제가 수정하겠습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmyeon 제가 PR을 작성했다가 삭제해서 그렇게 되었습니다.
혼동을 드려 죄송합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PDKhan
아하 그러셨군요!
리뷰 다시 한번 감사드려요 :)
1주차 문제 풀이 파이팅입니다!!


for (let i = 0; i < nums.length; i++) {
const neededValue = target - nums[i];
if (seenNumbers.has(neededValue)) {
return [i, seenNumbers.get(neededValue)!];
}

seenNumbers.set(nums[i], i);
}

return [];
}