diff --git a/combination-sum/yuseok89.py b/combination-sum/yuseok89.py new file mode 100644 index 000000000..6003c0fc2 --- /dev/null +++ b/combination-sum/yuseok89.py @@ -0,0 +1,28 @@ +# TC: O(N^T) +# SC: O(T) +class Solution: + + def rec(self, cur_idx, target, candi, cur_combi, ans): + + if target == 0: + ans.append(cur_combi.copy()) + + if cur_idx == len(candi): + return + + for idx in range(cur_idx, len(candi)): + if target < candi[idx]: + continue + + cur_combi.append(candi[idx]) + self.rec(idx, target - candi[idx], candi, cur_combi, ans) + cur_combi.pop() + + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + + ans = [] + + self.rec(0, target, candidates, [], ans) + + return ans + diff --git a/decode-ways/yuseok89.py b/decode-ways/yuseok89.py new file mode 100644 index 000000000..da6c0da32 --- /dev/null +++ b/decode-ways/yuseok89.py @@ -0,0 +1,29 @@ +# TC: O(N) +# SC: O(N) +class Solution: + + memo = [] + + def rec(self, idx, s, n): + if self.memo[idx] == -1: + + cnt = 0 + + if int(s[idx]) != 0: + cnt += self.rec(idx + 1, s, n) + + if idx + 1 < n: + if int(s[idx]) != 0 and int(s[idx]) * 10 + int(s[idx + 1]) <= 26: + cnt += self.rec(idx + 2, s, n) + + self.memo[idx] = cnt + + return self.memo[idx] + + def numDecodings(self, s: str) -> int: + + self.memo = [-1] * (len(s) + 1) + self.memo[len(s)] = 1 + + return self.rec(0, s, len(s)) + diff --git a/maximum-subarray/yuseok89.py b/maximum-subarray/yuseok89.py new file mode 100644 index 000000000..0056aae9b --- /dev/null +++ b/maximum-subarray/yuseok89.py @@ -0,0 +1,24 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + sum = nums[0] + ans = sum + + for num in nums[1:]: + sum = max(num, sum + num) + ans = max(ans, sum) + + return ans + +# class Solution: +# def maxSubArray(self, nums: List[int]) -> int: +# sum = nums[0] if nums[0] < 0 else 0 +# ans = sum + +# for num in nums: +# sum = max(num, sum + num) +# ans = max(ans, sum) + +# return ans + diff --git a/number-of-1-bits/yuseok89.py b/number-of-1-bits/yuseok89.py new file mode 100644 index 000000000..a70faf428 --- /dev/null +++ b/number-of-1-bits/yuseok89.py @@ -0,0 +1,13 @@ +# TC: O(logN) +# SC: O(1) +class Solution: + def hammingWeight(self, n: int) -> int: + + cnt = 0 + + while n > 0: + cnt = cnt + (n % 2) + n //= 2 + + return cnt + diff --git a/valid-palindrome/yuseok89.py b/valid-palindrome/yuseok89.py new file mode 100644 index 000000000..4af870bc8 --- /dev/null +++ b/valid-palindrome/yuseok89.py @@ -0,0 +1,43 @@ +# TC: O(N) +# SC: O(1) +class Solution: + + def is_alphanum(self, c): + return 'a' <= c <= 'z' or '0' <= c <= '9' + + def isPalindrome(self, s: str) -> bool: + + left = 0 + right = len(s) - 1 + + s = s.lower() + + while(left < right): + while left < len(s) and not self.is_alphanum(s[left]): + left += 1 + while right >= 0 and not self.is_alphanum(s[right]): + right -= 1 + + if left < right and s[left] != s[right]: + return False + + left += 1 + right -= 1 + + return True + +# class Solution: +# def isPalindrome(self, s: str) -> bool: +# +# import re +# +# s = re.sub(r'[^a-zA-Z0-9]', '', s.lower()) +# +# n = len(s) +# +# for idx in range(0, n // 2): +# if s[idx] != s[n - idx - 1]: +# return False +# +# return True +