diff --git a/3sum/hestia-park.py b/3sum/hestia-park.py new file mode 100644 index 000000000..565abffa3 --- /dev/null +++ b/3sum/hestia-park.py @@ -0,0 +1,25 @@ +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + answer=set() + for i in range(len(nums)): + if nums[i] > 0: + break # 이후 숫자는 모두 양수 → 합이 0 불가능 + if i > 0 and nums[i] == nums[i - 1]: + continue + + left, right = i + 1, len(nums) - 1 + while left < right: + term = nums[i] + nums[left] + nums[right] + if term == 0: + answer.add((nums[i], nums[left], nums[right])) + left += 1 + right -= 1 + elif term < 0: + left += 1 + else: + right -= 1 + + return [list(triplet) for triplet in answer] + + diff --git a/climbing-stairs/hestia-park.py b/climbing-stairs/hestia-park.py new file mode 100644 index 000000000..56bad6352 --- /dev/null +++ b/climbing-stairs/hestia-park.py @@ -0,0 +1,21 @@ +class Solution: + def climbStairs(self, n: int) -> int: + if n <= 1: + return 1 + + # dp = [0] * (n + 1) + # dp[0] = 1 + # dp[1] = 1 + + # for i in range(2, n + 1): + # dp[i] = dp[i - 1] + dp[i - 2] + + # return dp[n] + # optima sol + dp_0,dp_1=1,1 + for _ in range(2,n+1): + dp_0,dp_1=dp_1,dp_0+dp_1 + return dp_1 + + + diff --git a/contains-duplicate/hestia-park.py b/contains-duplicate/hestia-park.py new file mode 100644 index 000000000..d41e60eb0 --- /dev/null +++ b/contains-duplicate/hestia-park.py @@ -0,0 +1,5 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(nums) != len(set(nums)) + + diff --git a/product-of-array-except-self/hestia-park.py b/product-of-array-except-self/hestia-park.py new file mode 100644 index 000000000..e7f8c4e60 --- /dev/null +++ b/product-of-array-except-self/hestia-park.py @@ -0,0 +1,17 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n=len(nums) + answer=[1] * n + #for left + for i in range(1,n): + answer[i]=answer[i-1]*nums[i-1] + + tail=1 + #for right + for i in range(n-1,-1,-1): + answer[i] *= tail + tail *= nums[i] + return answer + + + diff --git a/valid-anagram/hestia-park.py b/valid-anagram/hestia-park.py new file mode 100644 index 000000000..dc1ae53fe --- /dev/null +++ b/valid-anagram/hestia-park.py @@ -0,0 +1,9 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + counter_s=Counter(s) + counter_t=Counter(t) + return counter_s == counter_t + + diff --git a/validate-binary-search-tree/hestia-park.py b/validate-binary-search-tree/hestia-park.py new file mode 100644 index 000000000..b4df31bb0 --- /dev/null +++ b/validate-binary-search-tree/hestia-park.py @@ -0,0 +1,12 @@ +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def check(node, min_val, max_val): + if not node: + return True + if not (min_val < node.val < max_val): + return False + return check(node.left, min_val, node.val) and check(node.right, node.val, max_val) + + return check(root, float('-inf'), float('inf')) + +