Skip to content
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
21 changes: 21 additions & 0 deletions find-minimum-in-rotated-sorted-array/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def findMin(self, nums: List[int]) -> int:
# Binary Search: compare mid with right
# nums[mid] > nums[right] -> min is in right half
# nums[mid] < nums[right] -> min is in left half (including mid)

left = 0
right = len(nums) - 1

while left < right:
mid = (left + right) // 2

if nums[mid] > nums[right]:
left = mid + 1
elif nums[mid] < nums[right]:
right = mid

return nums[left]

# Time Complexity: O(log n)
# Space Complexity: O(1)
20 changes: 20 additions & 0 deletions maximum-depth-of-binary-tree/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
# Recursive approach

if not root:
return 0

left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)

return max(left_depth, right_depth) + 1

# Time Complexity: O(n)
# Space Complexity: O(n)
30 changes: 30 additions & 0 deletions merge-two-sorted-lists/jylee2033.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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]:
# Use a while loop to compare values and build the merged list

# Create a new list
output = ListNode()
cur = output

while list1 and list2:
if list1.val < list2.val:
cur.next = list1
list1 = list1.next
else:
cur.next = list2
list2 = list2.next

cur = cur.next

# Attach the remaining nodes
cur.next = list1 or list2

return output.next

# Time Complexity : O(n + m), n - lenght of list1, m - length of list2
# Space Complexity : O(1)
Loading