Skip to content

Commit 7574a46

Browse files
author
sangbeenmoon
committed
solved combination-sums.
1 parent 8a3b803 commit 7574a46

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

combination-sum/sangbeenmoon.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# TC : O(2^target) 재귀 트리의 분기 수
2+
# SC : O(target/min(candidates)) 재귀 스택 깊이
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
self.answer = []
7+
for i in range(len(candidates)):
8+
self.go(i, candidates, [candidates[i]], candidates[i], target)
9+
10+
return self.answer
11+
12+
def go(self, i:int, candidates: List[int], nums: List[int], sum_: int, target: int):
13+
if sum_ == target:
14+
self.answer.append(nums.copy())
15+
16+
for j in range(i, len(candidates)):
17+
if candidates[j] + sum_ <= target:
18+
nums.append(candidates[j])
19+
self.go(j, candidates, nums, sum_ + candidates[j], target)
20+
nums.pop()
21+
22+
23+
24+
25+
26+

valid-palindrome/sangbeenmoon.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
4+
class Solution:
5+
def isPalindrome(self, s: str) -> bool:
6+
refined_s = []
7+
8+
for ch in s:
9+
if 'a' <= ch and ch <= 'z' or ('0' <= ch and ch <= '9'):
10+
refined_s.append(ch)
11+
continue
12+
13+
if 'A' <= ch and ch <= 'Z':
14+
refined_s.append(ch.lower())
15+
16+
for i in range(len(refined_s)):
17+
j = len(refined_s) - i - 1
18+
if refined_s[i] != refined_s[j]:
19+
return False
20+
21+
return True

0 commit comments

Comments
 (0)