diff --git a/combination-sum/yayyz.py b/combination-sum/yayyz.py new file mode 100644 index 000000000..a123d786a --- /dev/null +++ b/combination-sum/yayyz.py @@ -0,0 +1,16 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + result = [] + + def dfs(path, total, start): + if total == target: + result.append(path[:]) + return + if total > target: + return + + for i in range(start, len(candidates)): + dfs(path + [candidates[i]], total + candidates[i], i) + + dfs([], 0, 0) + return result diff --git a/decode-ways/yayyz.py b/decode-ways/yayyz.py new file mode 100644 index 000000000..9bdfb3390 --- /dev/null +++ b/decode-ways/yayyz.py @@ -0,0 +1,13 @@ +class Solution: + def numDecodings(self, s: str) -> int: + n = len(s) + dp = [0] * (n + 1) + dp[n] = 1 # 빈 문자열 처리 + + for i in range(n - 1, -1, -1): + if s[i] != "0": + dp[i] += dp[i + 1] + if i + 1 < n and int(s[i:i+2]) <= 26: + dp[i] += dp[i + 2] + + return dp[0] diff --git a/maximum-subarray/yayyz.py b/maximum-subarray/yayyz.py new file mode 100644 index 000000000..0648a302e --- /dev/null +++ b/maximum-subarray/yayyz.py @@ -0,0 +1,7 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + curr_sum = max_sum = nums[0] + for num in nums[1:]: + curr_sum = max(num, curr_sum + num) + max_sum = max(max_sum, curr_sum) + return max_sum diff --git a/number-of-1-bits/yayyz.py b/number-of-1-bits/yayyz.py new file mode 100644 index 000000000..d2ddce0c1 --- /dev/null +++ b/number-of-1-bits/yayyz.py @@ -0,0 +1,12 @@ +# bit operation +class Solution: + def hammingWeight(self, n: int) -> int: + count = 0 + while n: + count += n & 1 + n >>= 1 + return count + +# class Solution: +# def hammingWeight(self, n: int) -> int: +# return bin(n).count('1') diff --git a/valid-palindrome/yayyz.py b/valid-palindrome/yayyz.py new file mode 100644 index 000000000..8d4537da5 --- /dev/null +++ b/valid-palindrome/yayyz.py @@ -0,0 +1,15 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + s = ''.join(filter(str.isalnum, s)).lower() + if not (s and s.strip()): return True + + head = 0 + tail = len(s) -1 + while head < tail: + if s[head] != s[tail]: + return False + else: + head += 1 + tail -= 1 + + return True diff --git a/validate-binary-search-tree/yayyz.py b/validate-binary-search-tree/yayyz.py new file mode 100644 index 000000000..8764aa65d --- /dev/null +++ b/validate-binary-search-tree/yayyz.py @@ -0,0 +1,25 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def is_bst(node): + if not node: + return True, float('inf'), float('-inf') # (is_bst, min_val, max_val) + + left_valid, left_min, left_max = is_bst(node.left) + right_valid, right_min, right_max = is_bst(node.right) + + if not left_valid or not right_valid: + return False, 0, 0 + + if not (left_max < node.val < right_min): + return False, 0, 0 + + return True, min(left_min, node.val), max(right_max, node.val) + + valid, _, _ = is_bst(root) + return valid