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 diff --git a/jump-game/doh6077.py b/jump-game/doh6077.py new file mode 100644 index 0000000000..ecf9acd70e --- /dev/null +++ b/jump-game/doh6077.py @@ -0,0 +1,53 @@ +# 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 + + +# 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 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) +