-
-
Notifications
You must be signed in to change notification settings - Fork 363
[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
Open
JinuCheon
wants to merge
11
commits into
DaleStudy:main
Choose a base branch
from
JinuCheon:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−0
Open
Changes from all commits
Commits
Show all changes
11 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 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) | - |
피드백: 추가 공간 없이 두 포인터 방식으로 구현. 시간복잡도는 입력 문자열 길이에 비례합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.isPalindrome — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 공간을 추가로 사용해 cleaned 문자열을 만들고 역순으로 비교합니다. 추가 공간이 필요합니다.
개선 제안: 메모리 사용을 줄이고 싶다면 포인터 방식으로 대체하는 것이 좋습니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
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(n) / Space: O(1)피드백: 비트를 한 칸씩 확인하는 가장 기본적인 방법으로 구현되어 있습니다. 루프는 비트 길이에 비례합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.hammingWeight— Time: O(n) / Space: O(1)피드백: bin(n).count('1')를 사용해 구현했지만 내부적으로 문자열 생성이 필요해 실제 상수 공간은 아니며, 입력 비트 수에 비례하는 시간복잡도입니다.
개선 제안: 성능 측면에서 첫 번째 방법이 더 명확하고 메모리 사용이 예측 가능하므로 권장합니다.
풀이 3:
Solution.hammingWeight— Time: O(n) / Space: O(1)피드백: 비트 길이만큼 루프를 돌며 각 비트를 확인합니다. 간결하고 상수 공간.
개선 제안: 현재 구현이 적절해 보입니다.