-
-
Notifications
You must be signed in to change notification settings - Fork 363
[sangbeenmoon] WEEK 02 Solutions #2704
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
09f1ea0
745f661
94823b2
79a9055
667ffe0
912181e
c0c19ab
9a46ec6
b3a3b27
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 |
|---|---|---|
|
|
@@ -30,3 +30,36 @@ def threeSum(self, nums: list[int]) -> list[list[int]]: | |
| left = left + 1 | ||
| right = right - 1 | ||
| return answer | ||
|
|
||
|
|
||
| # TC : O(n^2) | ||
| # SC : O(n) | ||
|
|
||
| class Solution: | ||
| def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
|
|
||
| nums.sort() | ||
|
|
||
| answer_map = {} | ||
| answer = [] | ||
|
|
||
| for i in range(len(nums)): | ||
| target = -1 * nums[i] | ||
|
|
||
| left = i + 1 | ||
| right = len(nums) - 1 | ||
|
Comment on lines
+46
to
+50
Contributor
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. 정렬이 된 상태라 nums[i] 와 nums[i - 1] 이 같은 경우는 스킵해주고 nums[i] > 0 이 된 상태에서는 break 해주면 최적화가 가능할 거 같습니다 :) ex. [ -2, -1, -1, -1, 0, 1, 2, 3] , -1이 한번인 경우만 실행하고 이후는 스킵, nums[i]가 0보다 커지면 |
||
|
|
||
| while left < right: | ||
| if nums[left] + nums[right] == target: | ||
| candidate = (target * -1, nums[left], nums[right]) | ||
| if candidate not in answer_map: | ||
| answer_map[candidate] = True | ||
| answer.append([target * -1, nums[left], nums[right]]) | ||
|
Comment on lines
+52
to
+57
Contributor
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. 해시맵을 사용해서 candidate를 담아서 중복을 체크하셨군요!
Contributor
Author
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. 좀 더 깔끔하게 중복 체크할 수 있는 방법이 있었네요. 짚어주셔서 감사합니다 |
||
| left += 1 | ||
|
Comment on lines
+55
to
+58
Contributor
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. 성공한 경우 right -= 1 도 함께 해주면 좋을 거 같아요! |
||
| right -= 1 | ||
| elif nums[left] + nums[right] < target: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
| return answer | ||
|
|
||
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 가지 구현이 섞여 있는데, 첫 번째 구현은 전역 상태를 이용해 재귀적으로 범위를 검사한다. 두 번째는 재귀적으로 범위를 넘겨주는 일반적인 패턴으로 안정적이다. 개선 제안: 현재 구현이 적절해 보입니다.
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 후 외부 루프와 내부 투 포인터로 복잡도를 달성했다. 다만 중복 제거를 visited로 추가했는데, 전체적으로 불필요한 해시맵 사용 가능성이 있다. 두 번째 구현처럼 중복 제거를 더 간결하게 처리하는 편이 좋다.
개선 제안: 고려해볼 만한 대안: 중복 제거를 리스트 스킵 방식으로 처리하고 해시맵 없이도 중복 없는 결과를 얻도록 구현을 단순화