Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions number-of-1-bits/JinuCheon.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation, Division and Modulo, Binary Search
  • 설명: 핵심은 비트를 다루는 Bit Manipulation과 이진수 표현에서 1의 개수를 세는 방식으로 구현되어 있으며, n을 이진수로 다루는 다양한 방법을 사용한다. 그러나 본 문제에서 주로 비트 연산과 자릿수 접근이 중심이므로 Bit Manipulation이 가장 적합하다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 3가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.hammingWeight — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 비트를 한 칸씩 확인하는 가장 기본적인 방법으로 구현되어 있습니다. 루프는 비트 길이에 비례합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: Solution.hammingWeight — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: bin(n).count('1')를 사용해 구현했지만 내부적으로 문자열 생성이 필요해 실제 상수 공간은 아니며, 입력 비트 수에 비례하는 시간복잡도입니다.

개선 제안: 성능 측면에서 첫 번째 방법이 더 명확하고 메모리 사용이 예측 가능하므로 권장합니다.

풀이 3: Solution.hammingWeight — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 비트 길이만큼 루프를 돌며 각 비트를 확인합니다. 간결하고 상수 공간.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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
31 changes: 31 additions & 0 deletions valid-palindrome/JinuCheon.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Hash Map / Hash Set
  • 설명: 주 코드에는 좌우 포인터를 동시에 이동하는 Two Pointers 패턴이 핵심적으로 사용됩니다. 또한 두 번째 풀이에서 문자열을 클린징해 뒤집어 비교하는 방식은 기본적인 문자열 조작과 비교를 통해 해결하는 방식으로 보이며, 해시 맵/셋은 직접적으로 사용되진 않지만 isalnum(), lower() 등 문자 분리 및 비교를 이용한 효율적 접근으로 두 가지 풀이를 제공합니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.isPalindrome — Time: ✅ O(n) → O(n) / Space: O(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 문자열을 만들고 역순으로 비교합니다. 추가 공간이 필요합니다.

개선 제안: 메모리 사용을 줄이고 싶다면 포인터 방식으로 대체하는 것이 좋습니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

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]
Loading