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
34 changes: 34 additions & 0 deletions combination-sum/socow.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions number-of-1-bits/socow.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions valid-palindrome/socow.py
Original file line number Diff line number Diff line change
@@ -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]