From 93e950e44a73fb0aa1420b863abc7f7fa7c9383b Mon Sep 17 00:00:00 2001 From: doh6077 Date: Tue, 13 Jan 2026 12:49:19 -0500 Subject: [PATCH 1/4] 226. Invert Binary Tree Solution --- invert-binary-tree/doh6077.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 invert-binary-tree/doh6077.py diff --git a/invert-binary-tree/doh6077.py b/invert-binary-tree/doh6077.py new file mode 100644 index 0000000000..89cbdafe18 --- /dev/null +++ b/invert-binary-tree/doh6077.py @@ -0,0 +1,29 @@ +""" +226. Invert Binary Tree +https://leetcode.com/problems/invert-binary-tree/description/ + +Solution + Recursively swaps the left and right children of the root node. + +""" +# 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 + + +# Use DFS +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root is None: + return None + + temp = root.left + root.left = root.right + root.right = temp + + self.invertTree(root.left) + self.invertTree(root.right) + return root From d622f0cb67bae189dabc9409e01ff5ea4c390403 Mon Sep 17 00:00:00 2001 From: doh6077 Date: Tue, 13 Jan 2026 16:01:41 -0500 Subject: [PATCH 2/4] 246. Search in Rotated Sorted Array Solution --- search-in-rotated-sorted-array/doh6077.py | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 search-in-rotated-sorted-array/doh6077.py diff --git a/search-in-rotated-sorted-array/doh6077.py b/search-in-rotated-sorted-array/doh6077.py new file mode 100644 index 0000000000..77c765ea11 --- /dev/null +++ b/search-in-rotated-sorted-array/doh6077.py @@ -0,0 +1,71 @@ + + +#33. Search in Rotated Sorted Array +# First Try: use binary search but consider the rotated part + +# Solution: Find the minimum index where the rotation happens, then do binary search in the appropriate half. +class Solution: + def search(self, nums: List[int], target: int) -> int: + n = len(nums) + l = 0 + r = n - 1 + # 1. find the index of the smallest value using binary search. + while l < r: + m = (l + r) // 2 + + if nums[m] > nums[r]: + l = m + 1 + else: + r = m + + min_i = l + # 2. determine which part to do binary search on. + # First edge case: if the smallest index is 0, meaning the array is not rotated. + if min_i == 0: + l, r = 0, n - 1 + # Second edge case: if the target is the left part + elif target >= nums[0] and target <= nums[min_i - 1]: + l, r = 0, min_i - 1 + # Third edge case: if the target is the right part + else: + l, r = min_i, n - 1 + # 3. do binary search on the determined part. + while l <= r: + m = (l + r) // 2 + if nums[m] == target: + return m + elif nums[m] < target: + l = m + 1 + else: + r = m - 1 + return -1 + +# Time Complexity: O(log(n)) +# Space Complexity: O(1) + # if target not in nums: + # return -1 + + # # two cases: + # # 1. [left side], [right side] + # # if we figure out where is the boundary then we can just use binary search + + # def bst(target, start, end): + # if ( start > end): + # return -1 + + # mid = math.floor((end + start) / 2) + # print(nums[mid]) + # # 1. right side - sorted array + + # if nums[mid] == target: + # return mid + # elif ( nums[mid] < target) and nums[start] < nums[end]: + # return bst(target, mid+1, end) + # elif ( nums[mid] > target) and nums[start] < nums[end]: + # return bst(target, start, mid -1) + # elif ( nums[mid] < target) and nums[start] > nums[end]: + # return bst(target, start, mid -1) + # elif ( nums[mid] > target) and nums[start] > nums[end]: + # return bst(target, mid+1, end) + # return bst(target, 0, len(nums) -1) + From ebbed2467c6192f68104cf4c1a79195968edfbd3 Mon Sep 17 00:00:00 2001 From: doh6077 Date: Wed, 14 Jan 2026 16:20:34 -0500 Subject: [PATCH 3/4] Jump Game Solution --- jump-game/doh6077.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 jump-game/doh6077.py diff --git a/jump-game/doh6077.py b/jump-game/doh6077.py new file mode 100644 index 0000000000..3817e254dd --- /dev/null +++ b/jump-game/doh6077.py @@ -0,0 +1,36 @@ +# 55. Jump Game + +class Solution: + def canJump(self, nums: List[int]) -> bool: + + # Recursive + # Time: O(max(nums) ^ n) + # goal = len(nums) - 1 + + # def jump_recursive(index): + # if index >= goal: + # return True + # max_jump = nums[index] + # if max_jump == 0: + # return False + + # while max_jump > 0: + # if jump_recursive( index + max_jump): + # return True + # else: + # max_jump -= 1 + # return False + + # return jump_recursive(0) + + # Greedy - start at end + # Time : O(n) + + n = len(nums) + target = n - 1 + # -1 exclusive + for i in range(n-1, -1, -1): + max_jump = nums[i] + if i + max_jump >= target: + target = i + return target == 0 From f282432dac02134a16148cf8175883d6ed97096d Mon Sep 17 00:00:00 2001 From: doh6077 Date: Wed, 14 Jan 2026 16:35:35 -0500 Subject: [PATCH 4/4] Add another solution --- jump-game/doh6077.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/jump-game/doh6077.py b/jump-game/doh6077.py index 3817e254dd..ecf9acd70e 100644 --- a/jump-game/doh6077.py +++ b/jump-game/doh6077.py @@ -34,3 +34,20 @@ def canJump(self, nums: List[int]) -> bool: if i + max_jump >= target: target = i return target == 0 + + +# Greedy +class Solution: + def canJump(self, nums: List[int]) -> bool: + + + maxValue = 0 + reach = len(nums) - 1 + for i in range(0, len(nums)): + maxValue = max(nums[i] + i, maxValue) + if maxValue >= reach: return True + if maxValue == i and nums[i] == 0: + return False + + + return False