Skip to content

Commit cceeb81

Browse files
authored
Merge pull request #2271 from doh6077/main
[doh6077] WEEK 10 solutions
2 parents 6e85d0f + f282432 commit cceeb81

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

invert-binary-tree/doh6077.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
226. Invert Binary Tree
3+
https://leetcode.com/problems/invert-binary-tree/description/
4+
5+
Solution
6+
Recursively swaps the left and right children of the root node.
7+
8+
"""
9+
# Definition for a binary tree node.
10+
# class TreeNode:
11+
# def __init__(self, val=0, left=None, right=None):
12+
# self.val = val
13+
# self.left = left
14+
# self.right = right
15+
16+
17+
# Use DFS
18+
class Solution:
19+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
20+
if root is None:
21+
return None
22+
23+
temp = root.left
24+
root.left = root.right
25+
root.right = temp
26+
27+
self.invertTree(root.left)
28+
self.invertTree(root.right)
29+
return root

jump-game/doh6077.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 55. Jump Game
2+
3+
class Solution:
4+
def canJump(self, nums: List[int]) -> bool:
5+
6+
# Recursive
7+
# Time: O(max(nums) ^ n)
8+
# goal = len(nums) - 1
9+
10+
# def jump_recursive(index):
11+
# if index >= goal:
12+
# return True
13+
# max_jump = nums[index]
14+
# if max_jump == 0:
15+
# return False
16+
17+
# while max_jump > 0:
18+
# if jump_recursive( index + max_jump):
19+
# return True
20+
# else:
21+
# max_jump -= 1
22+
# return False
23+
24+
# return jump_recursive(0)
25+
26+
# Greedy - start at end
27+
# Time : O(n)
28+
29+
n = len(nums)
30+
target = n - 1
31+
# -1 exclusive
32+
for i in range(n-1, -1, -1):
33+
max_jump = nums[i]
34+
if i + max_jump >= target:
35+
target = i
36+
return target == 0
37+
38+
39+
# Greedy
40+
class Solution:
41+
def canJump(self, nums: List[int]) -> bool:
42+
43+
44+
maxValue = 0
45+
reach = len(nums) - 1
46+
for i in range(0, len(nums)):
47+
maxValue = max(nums[i] + i, maxValue)
48+
if maxValue >= reach: return True
49+
if maxValue == i and nums[i] == 0:
50+
return False
51+
52+
53+
return False
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
3+
#33. Search in Rotated Sorted Array
4+
# First Try: use binary search but consider the rotated part
5+
6+
# Solution: Find the minimum index where the rotation happens, then do binary search in the appropriate half.
7+
class Solution:
8+
def search(self, nums: List[int], target: int) -> int:
9+
n = len(nums)
10+
l = 0
11+
r = n - 1
12+
# 1. find the index of the smallest value using binary search.
13+
while l < r:
14+
m = (l + r) // 2
15+
16+
if nums[m] > nums[r]:
17+
l = m + 1
18+
else:
19+
r = m
20+
21+
min_i = l
22+
# 2. determine which part to do binary search on.
23+
# First edge case: if the smallest index is 0, meaning the array is not rotated.
24+
if min_i == 0:
25+
l, r = 0, n - 1
26+
# Second edge case: if the target is the left part
27+
elif target >= nums[0] and target <= nums[min_i - 1]:
28+
l, r = 0, min_i - 1
29+
# Third edge case: if the target is the right part
30+
else:
31+
l, r = min_i, n - 1
32+
# 3. do binary search on the determined part.
33+
while l <= r:
34+
m = (l + r) // 2
35+
if nums[m] == target:
36+
return m
37+
elif nums[m] < target:
38+
l = m + 1
39+
else:
40+
r = m - 1
41+
return -1
42+
43+
# Time Complexity: O(log(n))
44+
# Space Complexity: O(1)
45+
# if target not in nums:
46+
# return -1
47+
48+
# # two cases:
49+
# # 1. [left side], [right side]
50+
# # if we figure out where is the boundary then we can just use binary search
51+
52+
# def bst(target, start, end):
53+
# if ( start > end):
54+
# return -1
55+
56+
# mid = math.floor((end + start) / 2)
57+
# print(nums[mid])
58+
# # 1. right side - sorted array
59+
60+
# if nums[mid] == target:
61+
# return mid
62+
# elif ( nums[mid] < target) and nums[start] < nums[end]:
63+
# return bst(target, mid+1, end)
64+
# elif ( nums[mid] > target) and nums[start] < nums[end]:
65+
# return bst(target, start, mid -1)
66+
# elif ( nums[mid] < target) and nums[start] > nums[end]:
67+
# return bst(target, start, mid -1)
68+
# elif ( nums[mid] > target) and nums[start] > nums[end]:
69+
# return bst(target, mid+1, end)
70+
# return bst(target, 0, len(nums) -1)
71+

0 commit comments

Comments
 (0)