Skip to content
Merged
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, Divide and Conquer, Dynamic Programming, Hash Map / Hash Set, Two Pointers, Sliding Window, Fast & Slow Pointers, BFS, DFS, Backtracking, Dynamic Programming, Binary Search, Monotonic Stack, Heap / Priority Queue, Greedy, Union Find, Trie, Bit Manipulation
  • 설명: 주요 구현은 비트 연산과 이진수 표현 관련으로 Bit Manipulation 패턴에 해당합니다. 또한 간단한 루프를 이용해 각 비트를 확인하는 방식은 자리수 순회 형태의 Divide and Conquer적 특성도 보일 수 있습니다.

📊 시간/공간 복잡도 분석

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

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

피드백: 비트를 왼쪽부터 확인하는 방법으로 모든 비트를 확인하므로 입력 비트 수에 비례하는 시간 복잡도이다.

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

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

피드백: 파이썬의 내장 변환과 문자열 조회를 이용하므로 간결하지만, 최악의 경우 n 비트 처리에 해당한다.

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

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

피드백: 비트 시프트와 AND 연산으로 불필요한 연산 없이 한 비트씩 확인한다.

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

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

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, Monotonic Stack
  • 설명: 제시된 첫 번째 풀이에서 좌우 포인터를 이용해 양쪽에서 알파벳숫자만 비교하는 이중 포인터 방식이며, 두 번째 풀이도 문자열을 정리한 뒤 양쪽에서 비교하는 방식으로 부분적으로 같은 아이디어를 공유합니다. 두 풀이 모두 한 방향으로 인접 비교를 수행하는 점에서 Two Pointers 패턴에 속합니다.

📊 시간/공간 복잡도 분석

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

풀이 1: Solution.isPalindrome — Time: ✅ O(n) → O(n) / Space: O(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)

피드백: 공간을 추가로 사용해 간결하지만 전체 문자열을 새로 생성한다는 단점이 있다.

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

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

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