From 5d7e549657a94f1d09363d88395c6dc8fb2f92c0 Mon Sep 17 00:00:00 2001 From: helenapark0826 Date: Tue, 11 Feb 2025 03:01:29 -0500 Subject: [PATCH 1/5] solve: invertBinaryTree --- invert-binary-tree/yolophg.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 invert-binary-tree/yolophg.py diff --git a/invert-binary-tree/yolophg.py b/invert-binary-tree/yolophg.py new file mode 100644 index 000000000..503d3ff3c --- /dev/null +++ b/invert-binary-tree/yolophg.py @@ -0,0 +1,18 @@ +# Time Complexity: O(N) - visit each node once. +# Space Complexity: O(N) - skewed tree, recursion goes N levels deep. + +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root is None: + return root # If the tree is empty, just return None. + + # swap left and right child nodes. + root.left, root.right = root.right, root.left + + # recursively invert left and right subtrees. + if root.left: + self.invertTree(root.left) + if root.right: + self.invertTree(root.right) + + return root From c94b44c273a466ab22b5806bcc7e4aba09da0b41 Mon Sep 17 00:00:00 2001 From: helenapark0826 Date: Wed, 12 Feb 2025 02:14:55 -0500 Subject: [PATCH 2/5] solve: searchInRotatedSortedArray --- search-in-rotated-sorted-array/yolophg.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 search-in-rotated-sorted-array/yolophg.py diff --git a/search-in-rotated-sorted-array/yolophg.py b/search-in-rotated-sorted-array/yolophg.py new file mode 100644 index 000000000..79f19b135 --- /dev/null +++ b/search-in-rotated-sorted-array/yolophg.py @@ -0,0 +1,37 @@ +# Time Complexity: O(log n) - using a modified binary search, +# Space Complexity: O(1) - no extra space used, just a few variables. + +class Solution: + def search(self, nums: List[int], target: int) -> int: + # default result, in case the target isn't found + res = -1 + # set up the binary search range + left, right = 0, len(nums) - 1 + + while left <= right: + # calculate the middle index + mid = left + (right - left) // 2 + + # found the target, store the index + if nums[mid] == target: + res = mid + + # check if the left half is sorted + if nums[left] <= nums[mid]: + if nums[left] <= target < nums[mid]: + # target in the left half, move right pointer + right = mid - 1 + else: + # otherwise, search in the right half + left = mid + 1 + + # otherwise, the right half must be sorted + else: + if nums[mid] < target <= nums[right]: + # target in the right half, move left pointer + left = mid + 1 + else: + # otherwise, search in the left half + right = mid - 1 + + return res From dce73ca8edc7d873e1004e6b4310042beec94f13 Mon Sep 17 00:00:00 2001 From: helenapark0826 Date: Sat, 15 Feb 2025 22:49:15 -0500 Subject: [PATCH 3/5] solve: courseSchedule --- course-schedule/yolophg.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 course-schedule/yolophg.py diff --git a/course-schedule/yolophg.py b/course-schedule/yolophg.py new file mode 100644 index 000000000..629b8ca1c --- /dev/null +++ b/course-schedule/yolophg.py @@ -0,0 +1,43 @@ +# Time Complexity: O(V + E) - visit each course once and process all edges +# Space Complexity: O(V + E) - storing the graph + recursion stack (worst case) + +class Solution: + def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: + + # Build the adjacency list (prereq map) + preMap = {i: [] for i in range(numCourses)} + for crs, pre in prerequisites: + preMap[crs].append(pre) + + # tracks courses currently being checked (for cycle detection) + visited = set() + + def dfs(crs): + # found a cycle + if crs in visited: + return False + # no more prereqs left + if preMap[crs] == []: + return True + + # mark as visiting + visited.add(crs) + + # check all prereqs for this course + for pre in preMap[crs]: + if not dfs(pre): + # cycle detected, return False + return False + + # done checking, remove from visited + visited.remove(crs) + # mark as completed + preMap[crs] = [] + return True + + # run dfs on every course + for crs in range(numCourses): + if not dfs(crs): + return False + + return True From 31c9e1e87bca6aa7e4ac350bacffa4550c86cf64 Mon Sep 17 00:00:00 2001 From: yolophg Date: Sat, 15 Feb 2025 22:54:34 -0500 Subject: [PATCH 4/5] solve: jumpGame --- jump-game/yolophg.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 jump-game/yolophg.py diff --git a/jump-game/yolophg.py b/jump-game/yolophg.py new file mode 100644 index 000000000..49af40d72 --- /dev/null +++ b/jump-game/yolophg.py @@ -0,0 +1,29 @@ +# Time Complexity: O(n) - go through the array once, checking jump reachability. +# Space Complexity: O(1) - no extra space used, just a few variables. + +class Solution: + def canJump(self, nums: List[int]) -> bool: + + if len(nums) == 1: + # if there's only one element, we're already at the last index + return True + + # start from second-to-last index + backtrack_index = len(nums) - 2 + # need to jump at least once to reach the last index + jump = 1 + + while backtrack_index > 0: + # If we can jump from this position to the next "safe" spot + if nums[backtrack_index] >= jump: + # reset jump counter since we found a new reachable point + jump = 1 + else: + # otherwise, increase the jump required + jump += 1 + + # move one step left + backtrack_index -= 1 + + # finally, check if we can reach the last index from the starting position + return jump <= nums[0] From 7e678503d13e6ab311a7eda60fbffa34227c30c7 Mon Sep 17 00:00:00 2001 From: yolophg Date: Sat, 15 Feb 2025 23:00:50 -0500 Subject: [PATCH 5/5] solve: mergeKSortedLists --- merge-k-sorted-lists/yolophg.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 merge-k-sorted-lists/yolophg.py diff --git a/merge-k-sorted-lists/yolophg.py b/merge-k-sorted-lists/yolophg.py new file mode 100644 index 000000000..d0df67779 --- /dev/null +++ b/merge-k-sorted-lists/yolophg.py @@ -0,0 +1,22 @@ +# Time Complexity: O(N log N) - collecting all nodes is O(N), sorting is O(N log N) +# Space Complexity: O(N) - storing all nodes in an array + +class Solution: + def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: + + arr = [] + + # collect all nodes from the linked lists + for node in lists: + while node: + arr.append(node) + node = node.next + + # sort all nodes based on their value + arr.sort(key=lambda x: x.val) + + # reconnect nodes to form the merged linked list + for i in range(len(arr) - 1): + arr[i].next = arr[i + 1] + + return arr[0] if arr else None