Skip to content

Commit e65d4d4

Browse files
committed
week03_1_2_3_solution
1 parent f02e639 commit e65d4d4

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

combination-sum/YOOHYOJEONG.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://leetcode.com/problems/combination-sum/
2+
3+
# for문으로 해결이 되지 않아 gpt 도움을 받아 해결하였습니다.
4+
# 같은 숫자를 재사용하면서, 순서를 고정해서 중복 없는 조합을 DFS로 탐색하는 구조
5+
# 시간 복잡도 : O(2^n) (백트래킹 탐색)
6+
# 공간 복잡도 : O(target) (재귀 깊이)
7+
8+
class Solution(object):
9+
def combinationSum(self, candidates, target):
10+
11+
result = []
12+
13+
def dfs(start, path, total):
14+
# 종료 조건 1 : target을 맞춘 경우
15+
if total == target:
16+
result.append(path[:])
17+
return
18+
19+
# 종료 조건 2 : target 초과한 경우
20+
if total > target:
21+
return
22+
23+
# 후보 탐색 -> 같은 방향으로만 탐색
24+
for i in range(start, len(candidates)):
25+
# 같은 숫자 재사용 가능 -> i를 그대로 넘기기
26+
dfs(i, path+[candidates[i]], total+candidates[i]) # 새로운 리스트 만들어서 전달
27+
28+
29+
dfs(0, [], 0)
30+
31+
return result

number-of-1-bits/YOOHYOJEONG.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://leetcode.com/problems/number-of-1-bits
2+
3+
class Solution(object):
4+
def hammingWeight(self, n):
5+
binary = bin(n)[2:]
6+
return len(str(binary).replace('0', ''))

valid-palindrome/YOOHYOJEONG.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# https://leetcode.com/problems/valid-palindrome
2+
3+
# re.sub -> O(n)
4+
# lower() -> O(n)
5+
# reverse -> O(n)
6+
# 최종 시간 복잡도 : O(n) + O(n) + O(n) + O(n) = O(n)
7+
8+
# s_pali -> 최대 O(n)
9+
# s_pali_reverse -> O(n)
10+
# 최종 공간 복잡도 : O(n)
11+
12+
import re
13+
14+
class Solution(object):
15+
def isPalindrome(self, s):
16+
s_pali = re.sub(r'[^a-zA-Z0-9]', '', s).lower()
17+
s_pali_reverse = s_pali[::-1]
18+
19+
if s_pali == s_pali_reverse:
20+
return True
21+
else:
22+
return False

0 commit comments

Comments
 (0)