-
-
Notifications
You must be signed in to change notification settings - Fork 362
[JinuCheon] WEEK 03 Solutions #2731
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5768268
217. Contains Duplicate
JinuCheon 45f13c4
Two Sum
JinuCheon be11c1c
Merge remote-tracking branch 'upstream/main'
JinuCheon b174a0f
valid anagram
JinuCheon b606831
climbing stairs
JinuCheon 794ad7d
add line break
JinuCheon 37dc7f9
리뷰 반영: 점화식 적용하여 재귀호출 제거
JinuCheon 33a3efe
Product of Array Except Self
JinuCheon 69451cb
threeSum
JinuCheon 1d4dfa4
Merge branch 'DaleStudy:main' into main
JinuCheon c877776
3주차
JinuCheon 7847fc5
add line break
JinuCheon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 오래전 배운 2진수 구하는 방법을 그대로 적용. | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| cnt = 0 | ||
| while n > 0: | ||
| remainder = n % 2 | ||
| if remainder == 1: | ||
| cnt += 1 | ||
| n = n // 2 | ||
| return cnt | ||
|
|
||
| # LLM 이 알려준 다른 풀이. | ||
| # 그렇지만 라이브코딩테스트 등에서는 썩 적합하지 않은듯. | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| return bin(n).count('1') | ||
|
|
||
| # 또 다른 풀이. 비트연산 활용. | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| count = 0 | ||
| while n: | ||
| count += n & 1 # 맨 오른쪽 비트가 1인지 확인 | ||
| n >>= 1 # 오른쪽으로 한 칸 시프트 | ||
| return 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) | - |
피드백: 추가 배열이나 문자열 생성 없이 제일 효율적인 in-place 방식에 가깝다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.isPalindrome — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 공간을 추가로 사용해 간결하지만 전체 문자열을 새로 생성한다는 단점이 있다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # two point 풀이. | ||
| # 시간복잡도: O(n) | ||
| # 이중루프라서 시간복잡도 계산이 조금 헷갈린다. | ||
| # 추가공간을 쓰지 않는다. | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| leftCursor = 0 | ||
| rightCursor = len(s) - 1 | ||
|
|
||
| while leftCursor < rightCursor: | ||
| while leftCursor < rightCursor and not s[leftCursor].isalnum(): | ||
| leftCursor += 1 | ||
|
|
||
| while leftCursor < rightCursor and not s[rightCursor].isalnum(): | ||
| rightCursor -= 1 | ||
|
|
||
| if s[leftCursor].lower() != s[rightCursor].lower(): | ||
| return False | ||
|
|
||
| leftCursor += 1 | ||
| rightCursor -= 1 | ||
|
|
||
| return True | ||
|
|
||
| # 다른 풀이. | ||
| # 문자열 클리닝 & 뒤집어서 동등비교를 함. | ||
| # 간결하지만 추가공간 사용 -> 공간 O(n) | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| cleaned = ''.join(c.lower() for c in s if c.isalnum()) | ||
| return cleaned == cleaned[::-1] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.hammingWeight— Time: O(k) / Space: O(1)피드백: 비트를 왼쪽부터 확인하는 방법으로 모든 비트를 확인하므로 입력 비트 수에 비례하는 시간 복잡도이다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.hammingWeight— Time: O(k) / Space: O(1)피드백: 파이썬의 내장 변환과 문자열 조회를 이용하므로 간결하지만, 최악의 경우 n 비트 처리에 해당한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
Solution.hammingWeight— Time: O(k) / Space: O(1)피드백: 비트 시프트와 AND 연산으로 불필요한 연산 없이 한 비트씩 확인한다.
개선 제안: 현재 구현이 적절해 보입니다.