From fddf1155ea9c386b50435ea9af1b77ac7b00fe43 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 8 May 2024 11:40:22 -0500 Subject: [PATCH 1/5] =?UTF-8?q?[=EA=B9=80=EB=B3=91=ED=98=84,=20bhyun-kim]?= =?UTF-8?q?=20Week2=20Solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invert-binary-tree/bhyun-kim.py | 78 +++++++++++++++++++++++++++++ linked-list-cycle/bhyun-kim.py | 45 +++++++++++++++++ merge-two-sorted-lists/bhyun-kim.py | 42 ++++++++++++++++ reverse-linked-list/bhyun-kim.py | 40 +++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 invert-binary-tree/bhyun-kim.py create mode 100644 linked-list-cycle/bhyun-kim.py create mode 100644 merge-two-sorted-lists/bhyun-kim.py create mode 100644 reverse-linked-list/bhyun-kim.py diff --git a/invert-binary-tree/bhyun-kim.py b/invert-binary-tree/bhyun-kim.py new file mode 100644 index 000000000..e608cd76d --- /dev/null +++ b/invert-binary-tree/bhyun-kim.py @@ -0,0 +1,78 @@ +""" +226. Invert Binary Tree +https://leetcode.com/problems/invert-binary-tree/description/ + +Solution + Recursively swaps the left and right children of the root node. + + 1. Check if the root has left and right children. + 2. If it does, invert the left and right children. + 3. If it doesn't, invert the left or right child. + 4. Return the root. + +Time complexity: O(n) +Space complexity: O(n) + +""" +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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + has_left = hasattr(root, "left") + has_right = hasattr(root, "right") + + if has_left and has_right: + root.left = self.invertTree(root.left) + root.right = self.invertTree(root.right) + root.left, root.right = root.right, root.left + elif has_left: + root.left = self.invertTree(root.left) + root.left, root.right = None, root.left + elif has_right: + root.right = self.invertTree(root.right) + root.left, root.right = root.right, None + + return root + + +def main(): + test_cases = [ + [ + TreeNode( + 4, + TreeNode(2, TreeNode(1), TreeNode(3)), + TreeNode(7, TreeNode(6), TreeNode(9)), + ), + TreeNode( + 4, + TreeNode(7, TreeNode(9), TreeNode(6)), + TreeNode(2, TreeNode(3), TreeNode(1)), + ), + ], + [ + TreeNode(2, TreeNode(1), TreeNode(3)), + TreeNode(2, TreeNode(3), TreeNode(1)), + ], + [ + TreeNode(), + TreeNode(), + ], + ] + s = Solution() + + for test_case in test_cases: + root_input, expected = test_case + assert s.invertTree(root_input) == expected + + +if __name__ == "__main__": + main() diff --git a/linked-list-cycle/bhyun-kim.py b/linked-list-cycle/bhyun-kim.py new file mode 100644 index 000000000..2dc6fffde --- /dev/null +++ b/linked-list-cycle/bhyun-kim.py @@ -0,0 +1,45 @@ +""" +141. Linked List Cycle +https://leetcode.com/problems/linked-list-cycle/description/ + +Solution + Floyd's Tortoise and Hare algorithm: + + 1. Initialize two pointers, slower and faster, to the head of the linked list. + faster is one step ahead of slower. + 2. Move the slower pointer by one and the faster pointer by two. + 3. If the slower and faster pointers meet, return True. + 4. If the faster pointer reaches the end of the linked list, return False. + +Time complexity: O(n) +Space complexity: O(1) +""" + + +from typing import Optional + + +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + + +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + if not hasattr(head, "next"): + return False + + slower = head + faster = head.next + + while slower != faster: + if not hasattr(faster, "next"): + return False + elif not hasattr(faster.next, "next"): + return False + slower = slower.next + faster = faster.next.next + + return True diff --git a/merge-two-sorted-lists/bhyun-kim.py b/merge-two-sorted-lists/bhyun-kim.py new file mode 100644 index 000000000..5b09633b2 --- /dev/null +++ b/merge-two-sorted-lists/bhyun-kim.py @@ -0,0 +1,42 @@ +""" +21. Merge Two Sorted Lists +https://leetcode.com/problems/merge-two-sorted-lists/ + +Solution + Recursively merges two sorted linked lists. + + 1. Check if either list is empty. + 2. Compare the values of the two lists. + 3. Return a new node with the smaller value and the merged list. + +Time complexity: O(n) +Space complexity: O(n) +""" +from typing import Optional + + +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def mergeTwoLists( + self, list1: Optional[ListNode], list2: Optional[ListNode] + ) -> Optional[ListNode]: + if list1 is None: + return list2 + + if list2 is None: + return list1 + + if list1.val < list2.val: + val = list1.val + list1 = list1.next + else: + val = list2.val + list2 = list2.next + + return ListNode(val=val, next=self.mergeTwoLists(list1, list2)) diff --git a/reverse-linked-list/bhyun-kim.py b/reverse-linked-list/bhyun-kim.py new file mode 100644 index 000000000..2874bfa90 --- /dev/null +++ b/reverse-linked-list/bhyun-kim.py @@ -0,0 +1,40 @@ +""" +206. Reverse Linked List +https://leetcode.com/problems/reverse-linked-list/description/ + +Solution + Iteratively reverses a singly linked list. + + 1. Initialize two pointers, prev and curr, to None and the head of the linked list, respectively. + 2. While curr is not None: + a. Save the next node of curr. + b. Set the next node of curr to prev. + c. Move prev and curr to the next nodes. + +Time complexity: O(n) +Space complexity: O(1) + +""" + + +from typing import Optional + + +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + prev, curr = None, head + + while curr: + next_temp = curr.next + curr.next = prev + prev = curr + curr = next_temp + + return prev From 844d2025056bf6a0693bec0919ad2530151b1879 Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 8 May 2024 11:41:52 -0500 Subject: [PATCH 2/5] Remove solution part --- invert-binary-tree/bhyun-kim.py | 34 --------------------------------- 1 file changed, 34 deletions(-) diff --git a/invert-binary-tree/bhyun-kim.py b/invert-binary-tree/bhyun-kim.py index e608cd76d..fad1bede0 100644 --- a/invert-binary-tree/bhyun-kim.py +++ b/invert-binary-tree/bhyun-kim.py @@ -42,37 +42,3 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: root.left, root.right = root.right, None return root - - -def main(): - test_cases = [ - [ - TreeNode( - 4, - TreeNode(2, TreeNode(1), TreeNode(3)), - TreeNode(7, TreeNode(6), TreeNode(9)), - ), - TreeNode( - 4, - TreeNode(7, TreeNode(9), TreeNode(6)), - TreeNode(2, TreeNode(3), TreeNode(1)), - ), - ], - [ - TreeNode(2, TreeNode(1), TreeNode(3)), - TreeNode(2, TreeNode(3), TreeNode(1)), - ], - [ - TreeNode(), - TreeNode(), - ], - ] - s = Solution() - - for test_case in test_cases: - root_input, expected = test_case - assert s.invertTree(root_input) == expected - - -if __name__ == "__main__": - main() From bbf9378c97f9a677207206b8b59ed0a1554fb8bd Mon Sep 17 00:00:00 2001 From: bhyun-kim Date: Wed, 8 May 2024 16:28:28 -0500 Subject: [PATCH 3/5] Add solution of valid parenthese --- valid-parentheses/bhyun-kim.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 valid-parentheses/bhyun-kim.py diff --git a/valid-parentheses/bhyun-kim.py b/valid-parentheses/bhyun-kim.py new file mode 100644 index 000000000..44465dd87 --- /dev/null +++ b/valid-parentheses/bhyun-kim.py @@ -0,0 +1,43 @@ +""" +20. Valid Parentheses +https://leetcode.com/problems/valid-parentheses/description/ + +Solution + Uses a stack to check if the parentheses are valid. + + 1. Initialize an empty stack. + 2. Iterate through each character in the string. + 3. If the character is an opening parenthesis, + push it to the stack. + 4. If the character is a closing parenthesis, + pop the stack and check if the opening parenthesis matches. + 5. If the stack is empty, return True. + 6. Otherwise, return False. + +Time complexity: O(n) +Space complexity: O(n) +""" + + +class Solution: + def isValid(self, s: str) -> bool: + stack = "" + opening = ["(", "[", "{"] + closing = [")", "]", "}"] + + for _s in s: + if _s in opening: + stack = _s + stack + else: + if stack: + if opening[closing.index(_s)] == stack[0]: + stack = stack[1:] + else: + return False + else: + return False + + if len(stack) > 0: + return False + else: + return True From d9aaae6635227d92f75c6478039faecf528587ce Mon Sep 17 00:00:00 2001 From: bhyun-kim <94029750+bhyun-kim@users.noreply.github.com> Date: Thu, 9 May 2024 11:40:28 -0500 Subject: [PATCH 4/5] Update invert-binary-tree/bhyun-kim.py Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- invert-binary-tree/bhyun-kim.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/invert-binary-tree/bhyun-kim.py b/invert-binary-tree/bhyun-kim.py index fad1bede0..0a7971a48 100644 --- a/invert-binary-tree/bhyun-kim.py +++ b/invert-binary-tree/bhyun-kim.py @@ -31,14 +31,10 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: has_right = hasattr(root, "right") if has_left and has_right: - root.left = self.invertTree(root.left) - root.right = self.invertTree(root.right) - root.left, root.right = root.right, root.left + root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) elif has_left: - root.left = self.invertTree(root.left) - root.left, root.right = None, root.left + root.left, root.right = None, self.invertTree(root.left) elif has_right: - root.right = self.invertTree(root.right) - root.left, root.right = root.right, None + root.left, root.right = self.invertTree(root.right), None return root From 6c2cb7ac7b16378a9ec9caaca21a7eaafc177616 Mon Sep 17 00:00:00 2001 From: bhyun-kim <94029750+bhyun-kim@users.noreply.github.com> Date: Thu, 9 May 2024 11:40:52 -0500 Subject: [PATCH 5/5] Update valid-parentheses/bhyun-kim.py Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- valid-parentheses/bhyun-kim.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/valid-parentheses/bhyun-kim.py b/valid-parentheses/bhyun-kim.py index 44465dd87..1a24f8170 100644 --- a/valid-parentheses/bhyun-kim.py +++ b/valid-parentheses/bhyun-kim.py @@ -37,7 +37,4 @@ def isValid(self, s: str) -> bool: else: return False - if len(stack) > 0: - return False - else: - return True + return len(stack) == 0