From 4eb4431dc7975409de995cf2a32da56ba7540c27 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 16 Dec 2024 12:11:56 +0900 Subject: [PATCH 1/6] Create KwonNayeon.py --- valid-anagram/KwonNayeon.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 valid-anagram/KwonNayeon.py diff --git a/valid-anagram/KwonNayeon.py b/valid-anagram/KwonNayeon.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/valid-anagram/KwonNayeon.py @@ -0,0 +1 @@ + From 9a78a481b42dd4f2c232c59a0b8bc02d993c96bf Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 16 Dec 2024 12:13:19 +0900 Subject: [PATCH 2/6] Add placeholder file for 'Valid Anagram' problem --- valid-anagram/KwonNayeon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/valid-anagram/KwonNayeon.py b/valid-anagram/KwonNayeon.py index 8b1378917..139597f9c 100644 --- a/valid-anagram/KwonNayeon.py +++ b/valid-anagram/KwonNayeon.py @@ -1 +1,2 @@ + From a636a6764c6041a13e130a8d2814f8ee0047e9bf Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 16 Dec 2024 14:24:40 +0900 Subject: [PATCH 3/6] Solved 218. Valid Anagram using Python code --- valid-anagram/KwonNayeon.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/valid-anagram/KwonNayeon.py b/valid-anagram/KwonNayeon.py index 139597f9c..f5a78c0c5 100644 --- a/valid-anagram/KwonNayeon.py +++ b/valid-anagram/KwonNayeon.py @@ -1,2 +1,21 @@ +""" +Constraints: + - 1 <= len(s), len(t) <= 50_000 + - s and t consist of lowercase English letters (a-z) only +Time Complexity: + - O(n log n) +Space Complexity: + - O(n) +""" +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + + s = s.replace(' ', '').lower() + t = t.replace(' ', '').lower() + + if sorted(s) == sorted(t): + return True + else: + return False From 18e7dfab8b4ddf8adf8c1d46a1993b445fb52db7 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Mon, 16 Dec 2024 16:36:04 +0900 Subject: [PATCH 4/6] Solved 230. Climbing Stairs using Python code --- climbing-stairs/KwonNayeon.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 climbing-stairs/KwonNayeon.py diff --git a/climbing-stairs/KwonNayeon.py b/climbing-stairs/KwonNayeon.py new file mode 100644 index 000000000..054c8df0b --- /dev/null +++ b/climbing-stairs/KwonNayeon.py @@ -0,0 +1,23 @@ +""" +Constraints: + - 1 <= n <= 45 + +Time Complexity: + - O(n) +Space Complexity: + - O(n) +""" + +class Solution: + def climbStairs(self, n: int) -> int: + if n == 1: + return 1 + if n == 2: + return 2 + + dp = [0] * (n+1) + dp[1], dp[2] = 1, 2 + + for i in range(3, n+1): + dp[i] = dp[i-1] + dp[i-2] + return dp[n] From 0e4d1ef5c1fe56cee150abe78c8fed5a58dcda87 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Tue, 17 Dec 2024 19:32:16 +0900 Subject: [PATCH 5/6] Solved 241. 3Sum using Python code --- 3sum/KwonNayeon.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 3sum/KwonNayeon.py diff --git a/3sum/KwonNayeon.py b/3sum/KwonNayeon.py new file mode 100644 index 000000000..3e5852dfb --- /dev/null +++ b/3sum/KwonNayeon.py @@ -0,0 +1,42 @@ +""" +Constraints: +1. 3 <= nums.length <= 3000 +2. -10^5 <= nums[i] <= 10^5 + +Time Complexity: + - O(n^2) (정렬은 O(n log n), 이중 반복문은 O(n^2)) +Space Complexity: + - O(n) (결과 리스트) +""" + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + result = [] + + for i in range(len(nums) - 2): + if i > 0 and nums[i] == nums[i-1]: + continue + + left, right = i+1, len(nums)-1 + + while left < right: + sum = nums[i] + nums[left] + nums[right] + + if sum == 0: + result.append([nums[i], nums[left], nums[right]]) + + while left < right and nums[left] == nums[left+1]: + left += 1 + while left < right and nums[right] == nums[right-1]: + right -= 1 + + left += 1 + right -= 1 + + elif sum < 0: + left += 1 + else: + right -= 1 + + return result From 91937ec06e305391a0092d51ffc6f8a798a310ee Mon Sep 17 00:00:00 2001 From: Nayeon Date: Thu, 19 Dec 2024 18:03:20 +0900 Subject: [PATCH 6/6] Solved 253. Construct Binary Tree From Preorder And Inorder Traversal using Python code --- .../KwonNayeon.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/KwonNayeon.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/KwonNayeon.py b/construct-binary-tree-from-preorder-and-inorder-traversal/KwonNayeon.py new file mode 100644 index 000000000..d158f34ea --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/KwonNayeon.py @@ -0,0 +1,28 @@ +""" +Constraints: + 1. 1 <= preorder.length <= 3000 + 2. inorder.length == preorder.length + 3. -3000 <= preorder[i], inorder[i] <= 3000 + 4. preorder and inorder consist of unique values + 5. Each value of inorder also appears in preorder + 6. preorder is guaranteed to be the preorder traversal of the tree + 7. inorder is guaranteed to be the inorder traversal of the tree + +Time Complexity: + - O(N^2). 각 노드(N)마다 inorder에서 index를 찾는 연산(N)이 필요하고, 각 노드를 한 번씩 방문하여 트리를 구성하기 때문. +Space Complexity: + - O(N). 재귀 호출 스택을 위한 공간이 필요하며, 최악의 경우(한쪽으로 치우친 트리) 재귀 깊이가 N까지 갈 수 있기 때문. +""" + +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if not preorder or not inorder: + return None + + root = TreeNode(preorder[0]) + mid = inorder.index(preorder[0]) + + root.left = self.buildTree(preorder[1:mid+1], inorder[:mid]) + root.right = self.buildTree(preorder[mid+1:], inorder[mid+1:]) + + return root