Skip to content

[Helena] Week 10 #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions course-schedule/yolophg.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions invert-binary-tree/yolophg.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions jump-game/yolophg.py
Original file line number Diff line number Diff line change
@@ -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]
22 changes: 22 additions & 0 deletions merge-k-sorted-lists/yolophg.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions search-in-rotated-sorted-array/yolophg.py
Original file line number Diff line number Diff line change
@@ -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