-
-
Notifications
You must be signed in to change notification settings - Fork 363
[dolphinflow86] WEEK 03 Solutions #2710
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
060958d
2382072
13c1784
097a865
aea7ef4
dce7d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬된 후보를 이용해 남은 합이 현재 값보다 작아지면 가지치기를 수행한다. 재귀 경로의 길이를 저장하는 path와 답을 저장하는 answer의 공간이 주된 추가 공간이다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 1) Backtrack every possible combination to find subsets that match the target condition. | ||
| # TC: O(N^(T/M)) where N is the length of candidates, M is the minimum value in candidates, and T is the target number. | ||
| # SC: O(T/M) - The maximum depth of the recursion stack. | ||
| class Solution: | ||
| def backtrack(self, candidates: List[int], remain: int, start_index: int, path:List[int], answer: List[List[int]]): | ||
| if remain == 0: | ||
| answer.append(path[:]) | ||
| return | ||
|
|
||
| cur = candidates[start_index] | ||
|
|
||
| for i in range(start_index, len(candidates)): | ||
| if remain < candidates[i]: | ||
| break | ||
|
|
||
| path.append(candidates[i]) | ||
| self.backtrack(candidates, remain - candidates[i], i, path, answer) | ||
| path.pop() | ||
|
|
||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| path = [] | ||
| answer = [] | ||
|
|
||
| candidates.sort() | ||
| self.backtrack(candidates, target, 0, path, answer) | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 루프는 비트 길이만큼 실행되므로 시간은 로그 스케일이다. 초기값과 루프 조건을 단순화하면 좋다. 개선 제안: n을 비트 잉여 없이 고쳐 계산하는 내장 기법(예: Brian Kernighan's 알고리즘)으로 더 간결하게 만들 수 있다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # 1) Divide by 2 and count set bit. | ||
| # TC: O(logN) where N is the given number. | ||
| # SC: O(1) | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| setbit_count = 1 | ||
|
|
||
| while n >= 2: | ||
| if n % 2 == 1: setbit_count += 1 | ||
| n = n // 2 | ||
| return setbit_count |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(N) | O(n) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 두 구현 모두 필요 문자를 건너뛰고 비교하는 순서를 잘 다룬다. 공간은 상수다.
개선 제안: 필요 시 필터링 버전을 선택적 사용하도록 구조를 정리하면 코드 중복을 줄일 수 있다.
풀이 2: Solution.isPalindrome_filtered — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 추가 문자열 공간이 필요하지만 구현이 간결하다.
개선 제안: 공간 복잡도를 줄이려면 인덱스 방식의 양방향 탐색으로 구현하는 것이 좋다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # 1) Iterate through the string using two-pointers, skipping non-alphanumeric characters and comparing them in lowercase to validate the palindrome in-place. | ||
| # TC: O(N) where N is the length of s | ||
| # SC: O(1) | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| left = 0 | ||
| right = len(s) - 1 | ||
|
|
||
| while left < right: | ||
| if not s[left].isalnum(): | ||
| left += 1 | ||
| continue | ||
| if not s[right].isalnum(): | ||
| right -= 1 | ||
| continue | ||
|
|
||
| if s[left].lower() != s[right].lower(): return False | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| return True | ||
|
|
||
| # 2) Filter alphanumeric characters and conver them to lowercase to create a new string and then simply validate palindrome using two-pointers | ||
| # TC: O(N) where N is the length of s | ||
| # SC: O(N) where N is the length of s | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| new_str = "" | ||
| for ch in s: | ||
| if ch.isalnum(): | ||
| new_str += ch.lower() | ||
|
|
||
| n = len(new_str) | ||
| left = 0 | ||
| right = n - 1 | ||
|
|
||
| while left < right: | ||
| if new_str[left] != new_str[right]: return False | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| return True |
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.
@DaleStudy/coach 주석에 복잡도 추가 완료
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.
좋아요! 주석에 복잡도 추가를 반영하신 내용 확인했습니다.
다음 팁을 참고하면 더 품질이 좋아집니다.
질문이 있다면 어떤 파일의 주석 포맷이 리뷰어들에게 더 선명하게 다가오는지 함께 확인해드립니다.