-
-
Notifications
You must be signed in to change notification settings - Fork 362
[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 all commits
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 |
|---|---|---|
| @@ -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) | ✅ |
피드백: 첫 번째 해결법은 불필요한 문자열 복사를 피하고 제자리 비교를 통해 공간을 절약한다. 두 포인터로 한 번의 순회로 충분하다.
개선 제안: 현재 구현은 주석이 길지만, 실제 동작은 효율적이다. 필요 시 isalnum과 lower를 함께 사용한 조건식을 간결하게 다듬을 수 있다.
풀이 2: Solution.isPalindrome — Time: ✅ O(N) → O(n) / Space: ✅ O(N) → O(n)
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(N) | O(n) | ✅ |
| Space | O(N) | 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정수 n을 2로 나누어 가며 각 단계에서 나머지가 1인지 확인해 1의 개수를 증가시키는 방식이다.
개선 제안: 현재 구현은 조건과 루프의 초기 값 설정에 비효율이 있으며, 표준 방법인 Brian Kernighan의 알고리즘이나 비트 연산 루프를 사용하는 것이 더 간결하고 빠르다.