From e83302729e7199c68fdf83fbaae84d1cb329ad38 Mon Sep 17 00:00:00 2001 From: yayyz Date: Mon, 14 Apr 2025 10:32:06 +0900 Subject: [PATCH 1/7] valid palindrome solution --- valid-palindrome/yayyz.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 valid-palindrome/yayyz.py 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 From 002a743c6838584f770a81c8b647ba74fd2a5ba8 Mon Sep 17 00:00:00 2001 From: yayyz Date: Tue, 15 Apr 2025 21:28:29 +0900 Subject: [PATCH 2/7] number-of-1-bits solution --- number-of-1-bits/yayyz.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 number-of-1-bits/yayyz.py 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') From 53a3fd3c9fd70e673b4a037ffad56d591a9bdde2 Mon Sep 17 00:00:00 2001 From: yayyz Date: Fri, 18 Apr 2025 22:10:37 +0900 Subject: [PATCH 3/7] combination-sum solution --- combination-sum/yayyz.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 combination-sum/yayyz.py 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 From c5c85485748876f0c1f9d8f76cb90cef09761844 Mon Sep 17 00:00:00 2001 From: yayyz Date: Fri, 18 Apr 2025 22:14:16 +0900 Subject: [PATCH 4/7] decode-ways solution --- decode-ways/yayyz.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 decode-ways/yayyz.py 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] From 2083c157c298acf49b1b9de75a3620f3f9ddfae0 Mon Sep 17 00:00:00 2001 From: yayyz Date: Fri, 18 Apr 2025 22:16:26 +0900 Subject: [PATCH 5/7] maximum-subarray solution --- maximum-subarray/yayyz.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 maximum-subarray/yayyz.py 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 From 16e62ea78fc3551cb8db8dfef6d0c12cde0380dc Mon Sep 17 00:00:00 2001 From: yayyz Date: Fri, 18 Apr 2025 22:20:54 +0900 Subject: [PATCH 6/7] validate-binary-search-tree solution --- validate-binary-search-tree/yayyz.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 validate-binary-search-tree/yayyz.py diff --git a/validate-binary-search-tree/yayyz.py b/validate-binary-search-tree/yayyz.py new file mode 100644 index 000000000..178a44da5 --- /dev/null +++ b/validate-binary-search-tree/yayyz.py @@ -0,0 +1,28 @@ +# 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 + + + \ No newline at end of file From 0bc5a5d40396f56de5bba04adc1cb9fc8de4dcca Mon Sep 17 00:00:00 2001 From: yayyz Date: Fri, 18 Apr 2025 22:22:13 +0900 Subject: [PATCH 7/7] added new line --- validate-binary-search-tree/yayyz.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/validate-binary-search-tree/yayyz.py b/validate-binary-search-tree/yayyz.py index 178a44da5..8764aa65d 100644 --- a/validate-binary-search-tree/yayyz.py +++ b/validate-binary-search-tree/yayyz.py @@ -22,7 +22,4 @@ def is_bst(node): return True, min(left_min, node.val), max(right_max, node.val) valid, _, _ = is_bst(root) - return valid - - - \ No newline at end of file + return valid