diff --git a/3sum/yuseok89.py b/3sum/yuseok89.py new file mode 100644 index 0000000000..52d477339f --- /dev/null +++ b/3sum/yuseok89.py @@ -0,0 +1,33 @@ +# TC: O(N^2) +# SC: O(N) +class Solution: + def threeSum(self, nums: list[int]) -> list[list[int]]: + + cnt_dict = Counter(nums) + nums_uniq = sorted(cnt_dict.keys()) + n = len(nums_uniq) + + ret = [] + + for num in cnt_dict.keys(): + if num == 0: + if cnt_dict[num] >= 3: + ret.append([0, 0, 0]) + elif cnt_dict[num] >= 2 and num * 2 * -1 in cnt_dict: + ret.append([num, num, num * 2 * -1]) + + for idx1, num1 in enumerate(nums_uniq): + if num1 > 0: + break + + for idx2 in range(idx1 + 1, n): + num2 = nums_uniq[idx2] + num3 = (num1 + num2) * -1 + + if num2 >= num3: + break + elif num3 in cnt_dict: + ret.append([num1, num2, num3]) + + return ret + diff --git a/climbing-stairs/yuseok89.py b/climbing-stairs/yuseok89.py new file mode 100644 index 0000000000..98908f9b6d --- /dev/null +++ b/climbing-stairs/yuseok89.py @@ -0,0 +1,15 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def climbStairs(self, n: int) -> int: + + one_down = 1 + two_down = 0 + + for _ in range(1, n): + cur = two_down + one_down + two_down = one_down + one_down = cur + + return two_down + one_down + diff --git a/product-of-array-except-self/yuseok89.py b/product-of-array-except-self/yuseok89.py new file mode 100644 index 0000000000..2acceaf3c5 --- /dev/null +++ b/product-of-array-except-self/yuseok89.py @@ -0,0 +1,25 @@ +# TC: O(N) +# SC: O(N) +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + + n = len(nums) + prefix_prod = [0] * n + suffix_prod = [0] * n + + prefix_prod[0] = nums[0] + suffix_prod[-1] = nums[-1] + + for idx in range(1, n): + prefix_prod[idx] = prefix_prod[idx - 1] * nums[idx] + suffix_prod[-idx - 1] = suffix_prod[-idx] * nums[-idx - 1] + + ret = [] + + ret.append(suffix_prod[1]) + for idx in range(1, n - 1): + ret.append(prefix_prod[idx - 1] * suffix_prod[idx + 1]) + ret.append(prefix_prod[n - 2]) + + return ret + diff --git a/valid-anagram/yuseok89.py b/valid-anagram/yuseok89.py new file mode 100644 index 0000000000..f8793092e7 --- /dev/null +++ b/valid-anagram/yuseok89.py @@ -0,0 +1,13 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + + if len(s) != len(t): + return False + + cnt_s = Counter(s) + cnt_t = Counter(t) + + return cnt_s == cnt_t + diff --git a/validate-binary-search-tree/yuseok89.py b/validate-binary-search-tree/yuseok89.py new file mode 100644 index 0000000000..0e9cc2015d --- /dev/null +++ b/validate-binary-search-tree/yuseok89.py @@ -0,0 +1,23 @@ +# TC: O(N) +# SC: O(H) +# 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 isValid(self, left_bound, right_bound, root): + if root is None: + return True + if left_bound is not None and left_bound >= root.val: + return False + if right_bound is not None and right_bound <= root.val: + return False + + return self.isValid(left_bound, root.val, root.left) and self.isValid(root.val, right_bound, root.right) + + def isValidBST(self, root: Optional[TreeNode]) -> bool: + return self.isValid(None, root.val, root.left) and self.isValid(root.val, None, root.right) +