From b34fb6d41dbd33827e0fb45a012915d55864842a Mon Sep 17 00:00:00 2001 From: Young Choi Date: Sat, 15 Nov 2025 13:49:27 -0800 Subject: [PATCH 1/4] [WHYjun] WEEK 02 solutions - (3/5) --- climbing-stairs/WHYjun.py | 20 +++++++++++++++ product-of-array-except-self/WHYjun.py | 35 ++++++++++++++++++++++++++ valid-anagram/WHYjun.py | 17 +++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 climbing-stairs/WHYjun.py create mode 100644 product-of-array-except-self/WHYjun.py create mode 100644 valid-anagram/WHYjun.py diff --git a/climbing-stairs/WHYjun.py b/climbing-stairs/WHYjun.py new file mode 100644 index 000000000..9fd9f8f88 --- /dev/null +++ b/climbing-stairs/WHYjun.py @@ -0,0 +1,20 @@ +class Solution: + def climbStairs(self, n: int) -> int: + dp = {} + return self.climbStairsRecursively(n, dp) + + def climbStairsRecursively(self, n: int, dp: Dict[int, int]): + if n == 1: + dp[1] = 1 + return dp[1] + if n == 2: + dp[2] = 2 + return dp[2] + + if n - 2 not in dp: + dp[n - 2] = self.climbStairsRecursively(n - 2, dp) + + if n - 1 not in dp: + dp[n - 1] = self.climbStairsRecursively(n - 1, dp) + + return dp[n - 2] + dp[n - 1] diff --git a/product-of-array-except-self/WHYjun.py b/product-of-array-except-self/WHYjun.py new file mode 100644 index 000000000..b78dcb83f --- /dev/null +++ b/product-of-array-except-self/WHYjun.py @@ -0,0 +1,35 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [1] * len(nums) + for i in range(len(nums)): + if i == 0: + continue + answer[i] = answer[i-1] * nums[i-1] + + for j in range(len(nums)-1, -1, -1): + if j == len(nums) - 1: + product = 1 + answer[j] *= product + product *= nums[j] + + return answer + + def productExceptSelfTwoArrays(self, nums: List[int]) -> List[int]: + leftProduct = [1] * len(nums) + rightProduct = [1] * len(nums) + + for i in range(len(nums)): + if i == 0: + continue + leftProduct[i] = leftProduct[i-1] * nums[i-1] + + for j in range(len(nums)-1, -1, -1): + if j == len(nums) - 1: + continue + rightProduct[j] = rightProduct[j+1] * nums[j+1] + + for i in range(len(nums)): + leftProduct[i] *= rightProduct[i] + + return leftProduct + diff --git a/valid-anagram/WHYjun.py b/valid-anagram/WHYjun.py new file mode 100644 index 000000000..8d94575b9 --- /dev/null +++ b/valid-anagram/WHYjun.py @@ -0,0 +1,17 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + sCount = {} + tCount = {} + + for i in range(len(s)): + sCount[s[i]] = sCount.get(s[i], 0) + 1 + tCount[t[i]] = tCount.get(t[i], 0) + 1 + + for char in sCount.keys(): + if sCount.get(char, -1) != tCount.get(char, -1): + return False + + return True From 0e4e164c39edc35c52601a425b64ca68a905e7c2 Mon Sep 17 00:00:00 2001 From: Young Choi Date: Sat, 15 Nov 2025 13:53:22 -0800 Subject: [PATCH 2/4] [WHYjun] WEEK 02 solutions - (4/5) --- 3sum/WHYjun.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 3sum/WHYjun.py diff --git a/3sum/WHYjun.py b/3sum/WHYjun.py new file mode 100644 index 000000000..0e0f22713 --- /dev/null +++ b/3sum/WHYjun.py @@ -0,0 +1,30 @@ +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + answer = [] + sortedNums = sorted(nums) + + for i in range(len(sortedNums)): + # skip if same to avoid dup + if i > 0 and sortedNums[i] == sortedNums[i-1]: + continue + + # use two pointers + j = i + 1 + k = len(sortedNums) - 1 + + while j < k: + total = sortedNums[i] + sortedNums[j] + sortedNums[k] + + if total == 0: + answer.append([sortedNums[i], sortedNums[j], sortedNums[k]]) + j += 1 + # skip if same to avoid dup + while sortedNums[j] == sortedNums[j-1] and j < k: + j += 1 + elif total < 0: + j += 1 + else: + k -= 1 + + return answer + \ No newline at end of file From f46a3701c2444d1128cb2770f48ab8d548ed3681 Mon Sep 17 00:00:00 2001 From: Young Choi Date: Sat, 15 Nov 2025 14:23:37 -0800 Subject: [PATCH 3/4] [WHYjun] WEEK 02 solutions - (5/5) --- validate-binary-search-tree/WHYjun.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 validate-binary-search-tree/WHYjun.py diff --git a/validate-binary-search-tree/WHYjun.py b/validate-binary-search-tree/WHYjun.py new file mode 100644 index 000000000..273a2da70 --- /dev/null +++ b/validate-binary-search-tree/WHYjun.py @@ -0,0 +1,17 @@ +# 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: + # https://stackoverflow.com/a/37300370 + return self.isValid(root, float('-inf'), float('inf')) + + def isValid(self, node: Optional[TreeNode], low: float, high: float): + if node is None: + return True + if node.val <= low or node.val >= high: + return False + return self.isValid(node.left, low, node.val) and self.isValid(node.right, node.val, high) From 1cba52b4af5cf9b43d1fd58d955bb2eafeeee376 Mon Sep 17 00:00:00 2001 From: Young Choi Date: Sat, 15 Nov 2025 14:24:35 -0800 Subject: [PATCH 4/4] [WHYjun] remove tabs --- 3sum/WHYjun.py | 1 - 1 file changed, 1 deletion(-) diff --git a/3sum/WHYjun.py b/3sum/WHYjun.py index 0e0f22713..5c902d523 100644 --- a/3sum/WHYjun.py +++ b/3sum/WHYjun.py @@ -27,4 +27,3 @@ def threeSum(self, nums: List[int]) -> List[List[int]]: k -= 1 return answer - \ No newline at end of file