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
11 changes: 11 additions & 0 deletions number-of-1-bits/dolphinflow86.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
  • 설명: 주어진 코드는 이진 분해를 통해 각 자릿수(비트)를 검사하며 1의 개수를 세는 방식으로 동작합니다. 비트 연산 대신 나눗셈으로 몫과 나머지를 이용해 자릿수를 확인하는 점에서 비트 조작의 아이디어를 활용한 숫자 분해 패턴에 속합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(logN) O(log n)
Space O(1) O(1)

피드백: 정수 n을 2로 나누어 가며 각 단계에서 나머지가 1인지 확인해 1의 개수를 증가시키는 방식이다.

개선 제안: 현재 구현은 조건과 루프의 초기 값 설정에 비효율이 있으며, 표준 방법인 Brian Kernighan의 알고리즘이나 비트 연산 루프를 사용하는 것이 더 간결하고 빠르다.

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
42 changes: 42 additions & 0 deletions valid-palindrome/dolphinflow86.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
  • 설명: 주어진 코드는 문자열 양 끝에서 포인터를 이동하며 대소문자 무시 및 비알파뉴멋 제거를 병합/검증하는 방식으로 회문 여부를 판단한다. 두 포인터를 활용하는 대표적인 패턴에 해당한다.

📊 시간/공간 복잡도 분석

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

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