diff --git a/find-minimum-in-rotated-sorted-array/jylee2033.py b/find-minimum-in-rotated-sorted-array/jylee2033.py new file mode 100644 index 0000000000..3e5821afbe --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/jylee2033.py @@ -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) diff --git a/maximum-depth-of-binary-tree/jylee2033.py b/maximum-depth-of-binary-tree/jylee2033.py new file mode 100644 index 0000000000..575d96dd19 --- /dev/null +++ b/maximum-depth-of-binary-tree/jylee2033.py @@ -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) diff --git a/merge-two-sorted-lists/jylee2033.py b/merge-two-sorted-lists/jylee2033.py new file mode 100644 index 0000000000..439dd983e0 --- /dev/null +++ b/merge-two-sorted-lists/jylee2033.py @@ -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)