From 43f5f8f9144a1de08e866def2a667fbc6868d2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=ED=95=B4?= Date: Sat, 29 Nov 2025 17:56:39 +0900 Subject: [PATCH 1/4] =?UTF-8?q?3=EC=A3=BC=EC=B0=A8=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- combination-sum/Donghae0230.py | 16 ++++++++++++++++ number-of-1-bits/Donghae0230.py | 19 +++++++++++++++++++ valid-palindrome/Donghae0230.py | 12 ++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 combination-sum/Donghae0230.py create mode 100644 number-of-1-bits/Donghae0230.py create mode 100644 valid-palindrome/Donghae0230.py diff --git a/combination-sum/Donghae0230.py b/combination-sum/Donghae0230.py new file mode 100644 index 0000000000..fe0e8b3143 --- /dev/null +++ b/combination-sum/Donghae0230.py @@ -0,0 +1,16 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + def backtrack(start, combination): + if sum(combination) > target: + return + if sum(combination) == target: + result.append(combination[:]) + return + for i in range(start, len(candidates)): + combination.append(candidates[i]) + backtrack(i, combination) + combination.pop() + + result = [] + backtrack(0, []) + return result diff --git a/number-of-1-bits/Donghae0230.py b/number-of-1-bits/Donghae0230.py new file mode 100644 index 0000000000..efcc86ee73 --- /dev/null +++ b/number-of-1-bits/Donghae0230.py @@ -0,0 +1,19 @@ +# 시간 복잡도 O(n): 2로 반복해서 나눠야함 +# 공간 복잡도 O(n): result 생성 + +class Solution: + def devide_by_2 (self, n, temp): + global val + if n > 1 : + val = n // 2 + remain = n % 2 + temp.append(remain) + return self.devide_by_2(val, temp) + temp.append(val) + return val, temp + + def hammingWeight(self, n: int) -> int: + result = [] + n, result = self.devide_by_2(n, result) + return result.count(1) + \ No newline at end of file diff --git a/valid-palindrome/Donghae0230.py b/valid-palindrome/Donghae0230.py new file mode 100644 index 0000000000..065c8b4b60 --- /dev/null +++ b/valid-palindrome/Donghae0230.py @@ -0,0 +1,12 @@ +# 시간복잡도 O(n): reversed 함수 사용 +# 공간복잡도 O(n): cleaned_s, reversed_s 사용 +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + cleaned_s = re.sub(r'[^a-zA-Z0-9]', '', s.lower()) + reversed_s = ''.join(reversed(cleaned_s)) + if cleaned_s == reversed_s: + return True + else: + return False From fea2311323e967476960fe98d6f4b8000d018eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=ED=95=B4?= Date: Sat, 29 Nov 2025 18:34:46 +0900 Subject: [PATCH 2/4] python lint --- combination-sum/Donghae0230.py | 1 + number-of-1-bits/Donghae0230.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/combination-sum/Donghae0230.py b/combination-sum/Donghae0230.py index fe0e8b3143..48627ee0dd 100644 --- a/combination-sum/Donghae0230.py +++ b/combination-sum/Donghae0230.py @@ -14,3 +14,4 @@ def backtrack(start, combination): result = [] backtrack(0, []) return result + diff --git a/number-of-1-bits/Donghae0230.py b/number-of-1-bits/Donghae0230.py index efcc86ee73..9b2092b7ee 100644 --- a/number-of-1-bits/Donghae0230.py +++ b/number-of-1-bits/Donghae0230.py @@ -16,4 +16,3 @@ def hammingWeight(self, n: int) -> int: result = [] n, result = self.devide_by_2(n, result) return result.count(1) - \ No newline at end of file From a50afb7c288ccfbafa51982f8364b6a733bf93bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=ED=95=B4?= Date: Sat, 29 Nov 2025 23:42:18 +0900 Subject: [PATCH 3/4] =?UTF-8?q?3=EC=A3=BC=EC=B0=A8=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- combination-sum/Donghae0230.py | 5 +++++ number-of-1-bits/Donghae0230.py | 23 +++++++++++++---------- valid-palindrome/Donghae0230.py | 8 ++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/combination-sum/Donghae0230.py b/combination-sum/Donghae0230.py index 48627ee0dd..0a293c8ab2 100644 --- a/combination-sum/Donghae0230.py +++ b/combination-sum/Donghae0230.py @@ -1,3 +1,8 @@ +# 문제 풀이 +# 모든 경우의 수를 탐색하기 위해 백트래킹 사용 +# - 현재 조합의 합이 target보다 크면 종료 +# - 현재 조합의 합이 target과 같으면 결과에 추가 + class Solution: def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: def backtrack(start, combination): diff --git a/number-of-1-bits/Donghae0230.py b/number-of-1-bits/Donghae0230.py index 9b2092b7ee..8bb53082d5 100644 --- a/number-of-1-bits/Donghae0230.py +++ b/number-of-1-bits/Donghae0230.py @@ -1,18 +1,21 @@ -# 시간 복잡도 O(n): 2로 반복해서 나눠야함 -# 공간 복잡도 O(n): result 생성 +# 문제 풀이 +# 1. 입력값 n을 binary 형태로 변환 +# - 입력값 n이 1보다 크면 2로 나눠 몫과 나머지 계산 +# - 나머지를 리스트에 추가해 반환 +# 2. 반환된 리스트에서 1의 갯수 반환 + +# 시간복잡도 O(log n): n을 2로 나누면서 재귀 함수 실행 +# 공간복잡도 O(log n): 비트를 저장하는 리스트의 길이 class Solution: def devide_by_2 (self, n, temp): - global val if n > 1 : - val = n // 2 - remain = n % 2 - temp.append(remain) - return self.devide_by_2(val, temp) - temp.append(val) - return val, temp + temp.append(n % 2) + return self.devide_by_2(n // 2, temp) + temp.append(1) + return n, temp def hammingWeight(self, n: int) -> int: result = [] n, result = self.devide_by_2(n, result) - return result.count(1) + return result.count(1) \ No newline at end of file diff --git a/valid-palindrome/Donghae0230.py b/valid-palindrome/Donghae0230.py index 065c8b4b60..adb74cff68 100644 --- a/valid-palindrome/Donghae0230.py +++ b/valid-palindrome/Donghae0230.py @@ -1,5 +1,9 @@ -# 시간복잡도 O(n): reversed 함수 사용 -# 공간복잡도 O(n): cleaned_s, reversed_s 사용 +# 문제 풀이 +# 1. 문자열을 소문자로 변환 후 문자와 숫자가 아닌 값을 제거 +# 2. 문자열을 뒤집은 후 원래 문자열과 비교 + +# 시간복잡도 O(n): 문자열 처리(re.sub, reversed 등) 사용 +# 공간복잡도 O(n): 원래 문자열 만큼의 공간 사용 import re class Solution: From e99bf843bec51461036db798b7a044fdbf97f021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=ED=95=B4?= Date: Sat, 29 Nov 2025 23:44:52 +0900 Subject: [PATCH 4/4] lint --- combination-sum/Donghae0230.py | 1 - number-of-1-bits/Donghae0230.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/combination-sum/Donghae0230.py b/combination-sum/Donghae0230.py index 0a293c8ab2..57e24072bb 100644 --- a/combination-sum/Donghae0230.py +++ b/combination-sum/Donghae0230.py @@ -19,4 +19,3 @@ def backtrack(start, combination): result = [] backtrack(0, []) return result - diff --git a/number-of-1-bits/Donghae0230.py b/number-of-1-bits/Donghae0230.py index 8bb53082d5..7b91c45c6d 100644 --- a/number-of-1-bits/Donghae0230.py +++ b/number-of-1-bits/Donghae0230.py @@ -18,4 +18,4 @@ def devide_by_2 (self, n, temp): def hammingWeight(self, n: int) -> int: result = [] n, result = self.devide_by_2(n, result) - return result.count(1) \ No newline at end of file + return result.count(1)