From d5e5a440838efb52d0a10fdb5d8f31b2f330f41e Mon Sep 17 00:00:00 2001 From: socow Date: Tue, 25 Nov 2025 12:15:25 +0900 Subject: [PATCH 1/3] =?UTF-8?q?valid-palindrome=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-palindrome/socow.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 valid-palindrome/socow.py diff --git a/valid-palindrome/socow.py b/valid-palindrome/socow.py new file mode 100644 index 0000000000..d7f33c768e --- /dev/null +++ b/valid-palindrome/socow.py @@ -0,0 +1,15 @@ +""" + 문제 요약 + - 영숫자만 고려, 대소문자 무시 → 팰린드롬 여부 + + 아이디어 + - 소문자 변환 → 영숫자만 필터 → 역순과 동일한지 비교 + + 시간복잡도: O(n) + 공간복잡도: O(n) +""" + +class Solution: + def isPalindrome(self, s: str) -> bool: + cleaned = [c for c in s.lower() if c.isalnum()] + return cleaned == cleaned[::-1] \ No newline at end of file From f6ed285c424edbdfae9e5e24b5ca00cf5982d1a2 Mon Sep 17 00:00:00 2001 From: socow Date: Tue, 25 Nov 2025 12:22:24 +0900 Subject: [PATCH 2/3] =?UTF-8?q?number-of-1-bits=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- number-of-1-bits/socow.py | 19 +++++++++++++++++++ valid-palindrome/socow.py | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 number-of-1-bits/socow.py diff --git a/number-of-1-bits/socow.py b/number-of-1-bits/socow.py new file mode 100644 index 0000000000..b45ceb274b --- /dev/null +++ b/number-of-1-bits/socow.py @@ -0,0 +1,19 @@ +""" + 문제 요약 + - 32비트 부호 없는 정수 n의 이진 표현에서 '1'의 개수(= set bits)를 구하라. + + 아이디어 A: 브라이언 커니핸(Brian Kernighan) 알고리즘 + - 핵심 관찰: n & (n - 1)은 n의 최하위 1비트를 '0'으로 만든다. + 예) n = 0b101100 → n-1 = 0b101011 → n & (n-1) = 0b101000 (최하위 1 하나 삭제) + - 따라서 '1'의 개수만큼 루프가 돈다. + - 시간복잡도: O(k) (k = n의 1비트 개수) ← 매우 빠름 + - 공간복잡도: O(1) +""" + +class Solution: + def hammingWeight(self, n: int) -> int: + cnt = 0 + while n: + n &= n - 1 # 최하위 1비트 제거 + cnt += 1 + return cnt diff --git a/valid-palindrome/socow.py b/valid-palindrome/socow.py index d7f33c768e..fc885c9576 100644 --- a/valid-palindrome/socow.py +++ b/valid-palindrome/socow.py @@ -12,4 +12,4 @@ class Solution: def isPalindrome(self, s: str) -> bool: cleaned = [c for c in s.lower() if c.isalnum()] - return cleaned == cleaned[::-1] \ No newline at end of file + return cleaned == cleaned[::-1] From faf6d9826007db5f6f7d4e1571a9aee086362f08 Mon Sep 17 00:00:00 2001 From: socow Date: Wed, 26 Nov 2025 14:39:11 +0900 Subject: [PATCH 3/3] combination-sum --- combination-sum/socow.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 combination-sum/socow.py diff --git a/combination-sum/socow.py b/combination-sum/socow.py new file mode 100644 index 0000000000..d82fb59d86 --- /dev/null +++ b/combination-sum/socow.py @@ -0,0 +1,34 @@ +""" + 문제 요약 + - candidates 배열에서 숫자를 무제한 사용하여 합이 target이 되는 모든 조합 찾기 + + 아이디어 + - 백트래킹: i번째 인덱스부터 탐색하여 중복 조합 방지 + - 같은 숫자 재사용 가능 → 재귀 시 인덱스 i 유지 + - 정렬 후 target 초과 시 break로 가지치기 + + 시간복잡도: O(N^(T/M)) - N: candidates 길이, T: target, M: 최소값 + 공간복잡도: O(T/M) - 재귀 깊이 +""" + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + sol = [] + candidates.sort() + n = len(candidates) + + def backtrack(start, cur_sum): + if cur_sum == target: + result.append(sol.copy()) + return + + for i in range(start, n): + if cur_sum + candidates[i] > target: + break + sol.append(candidates[i]) + backtrack(i, cur_sum + candidates[i]) + sol.pop() + + backtrack(0, 0) + return result