diff --git a/3sum/hellojoyworldz.py b/3sum/hellojoyworldz.py new file mode 100644 index 0000000000..c397f6c258 --- /dev/null +++ b/3sum/hellojoyworldz.py @@ -0,0 +1,36 @@ +# 문제: https://leetcode.com/problems/3sum/ +# 해설: https://www.algodale.com/problems/3sum/ +# 위치: https://github.com/DaleStudy/leetcode-study/tree/main/3sum + +from typing import List + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + result = [] + + for i in range(len(nums)): + if i > 0 and nums[i] == nums[i - 1]: + continue + + left, right = i + 1, len(nums) - 1 + while left < right: + total = nums[i] + nums[left] + nums[right] + + if total < 0: + left += 1 + elif total > 0: + right -= 1 + else: + result.append([nums[i], nums[left], nums[right]]) + left += 1 + right -= 1 + + while left < right and nums[left] == nums[left - 1]: + left += 1 + while left < right and nums[right] == nums[right + 1]: + right -= 1 + + return result + diff --git a/climbing-stairs/hellojoyworldz.py b/climbing-stairs/hellojoyworldz.py new file mode 100644 index 0000000000..e1146db506 --- /dev/null +++ b/climbing-stairs/hellojoyworldz.py @@ -0,0 +1,11 @@ +# 문제: https://leetcode.com/problems/climbing-stairs/ +# 해설: https://www.algodale.com/problems/climbing-stairs/ +# 위치: https://github.com/DaleStudy/leetcode-study/tree/main/climbing-stairs + +class Solution: + def climbStairs(self, n: int) -> int: + prev2, prev1 = 1, 1 + for _ in range(n - 1): + prev2, prev1 = prev1, prev2 + prev1 + return prev1 + diff --git a/product-of-array-except-self/hellojoyworldz.py b/product-of-array-except-self/hellojoyworldz.py new file mode 100644 index 0000000000..744d61f294 --- /dev/null +++ b/product-of-array-except-self/hellojoyworldz.py @@ -0,0 +1,24 @@ +# 문제: https://leetcode.com/problems/product-of-array-except-self/ +# 해설: https://www.algodale.com/problems/product-of-array-except-self/ +# 위치: https://github.com/DaleStudy/leetcode-study/tree/main/product-of-array-except-self + +from typing import List + + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + answer = [1] * n + + left = 1 + for i in range(n): + answer[i] = left + left *= nums[i] + + right = 1 + for i in reversed(range(n)): + answer[i] *= right + right *= nums[i] + + return answer + diff --git a/valid-anagram/hellojoyworldz.py b/valid-anagram/hellojoyworldz.py new file mode 100644 index 0000000000..6f973c36c2 --- /dev/null +++ b/valid-anagram/hellojoyworldz.py @@ -0,0 +1,8 @@ +# 문제: https://leetcode.com/problems/valid-anagram/ +# 해설: https://www.algodale.com/problems/valid-anagram/ +# 위치: https://github.com/DaleStudy/leetcode-study/tree/main/valid-anagram + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return sorted(s) == sorted(t) + diff --git a/validate-binary-search-tree/hellojoyworldz.py b/validate-binary-search-tree/hellojoyworldz.py new file mode 100644 index 0000000000..73ae81bdaf --- /dev/null +++ b/validate-binary-search-tree/hellojoyworldz.py @@ -0,0 +1,26 @@ +# 문제: https://leetcode.com/problems/validate-binary-search-tree/ +# 해설: https://www.algodale.com/problems/validate-binary-search-tree/ +# 위치: https://github.com/DaleStudy/leetcode-study/tree/main/validate-binary-search-tree + +from typing import Optional + +# 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 check(node: Optional[TreeNode], low: float, high: float) -> bool: + if not node: + return True + + if not (low < node.val < high): + return False + + return check(node.left, low, node.val) and check(node.right, node.val, high) + + return check(root, float("-inf"), float("inf")) +