Conversation
There was a problem hiding this comment.
Pull request overview
Adds the author’s TypeScript solution for LeetCode #217 (Contains Duplicate) to the repository’s per-problem solution set.
Changes:
- Added
containsDuplicateimplementation using aSetsize comparison (O(n) time / O(n) space).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Cyjin-jani
left a comment
There was a problem hiding this comment.
문제 풀이 고생 많으셨습니다!
남은 문제 & 다음주도 화이팅입니다💪💪
| function containsDuplicate(nums: number[]): boolean { | ||
| return new Set(nums).size !== nums.length; |
| const table = new Map(); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const complement = target - nums[i]; |
There was a problem hiding this comment.
유의미한 변수명을 사용해주셨네요..!👍👍👍
(사실 영알못이라 뜻을 잘 몰라서 찾아봤었습니다..😅)
| if (table.has(complement)) { | ||
| return [table.get(complement)!, i]; | ||
| } |
There was a problem hiding this comment.
Map의 get이 값 아니면 undefined를 반환해서 !를 붙여주신 것 같네요..! 👍👍
타입단언 없이도 할 수 있는 방법을 조금 고민해보았는데, 다음과 같이 작성해 볼 수도 있을 것 같습니다..!
| if (table.has(complement)) { | |
| return [table.get(complement)!, i]; | |
| } | |
| const complementValue = table.get(complement); | |
| if (complementValue !== undefined) { | |
| return [complementValue, i]; | |
| } |
There was a problem hiding this comment.
앗..! 그렇네요 다음번에는 if 조건문으로 확인하도록 하겠습니다!
| /* 개선점? | ||
| * Map 과 sort 이외의 방식이 없을까? | ||
| * Map 의 장점은 get 할 때 O(1) 인건데, sort 를 하면서 시간복잡도가 증가함. | ||
| */ |
There was a problem hiding this comment.
저랑 접근 방식이 다소 비슷해서 놀랐습니다 ㅎㅎ 저도 처음에 sort를 썼다가 시간복잡도가 증가해서 어떻게든 O(n)으로 줄이기 위해 2번째 시도와 거의 동일한 방법으로 해결했었습니다..!
| for (let i = bucket.length - 1; i >= 0; i--) { | ||
| // 값이 비어있지 않다면, 해당 인덱스 순서만큼 카운팅된 숫자 밸류를 의미 | ||
| if (bucket[i].length > 0) { | ||
| answer.push(...bucket[i]); |
There was a problem hiding this comment.
처음에 스프레드 연산자로 전부 push하면 갯수가 k보다 커질 수도 있으니 엣지 케이스가 발생하는 게 아닌가 하는 우려(?)가 있었는데요, 다음 제약조건 때문에 문제가 없는 것 같네요! 👍👍👍
(제약 조건을 좀 더 꼼꼼히 봐야겠다고 반성했습니다 😅)
It is guaranteed that the answer is unique.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!