Skip to content

Commit 0ca3158

Browse files
committed
add Search in Rotated Sorted Array
1 parent 1975481 commit 0ca3158

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Question (#33): Given an array that's sorted but is shifted at a given pivot, find the index of a given number
2+
# Solution: Use a regular binary search, but each time you land on the 'wrong side' of the array from the target, set that
3+
# value to be + or - infinity and do a regular binary search.
4+
# The idea is if our array is [5, 6, 7, 8, 1, 2, 3, 4], we can't do a binary search on it because of the rotation,
5+
# but if we know what side our target is we can make the array [-inf, -inf, -inf, -inf, 1, 2, 3, 4] or [5, 6, 7, 8, inf, inf, inf, inf]
6+
# this way we can continue to do a regular binary search.
7+
8+
def search(self, nums: List[int], target: int) -> int:
9+
l, r = 0, len(nums) - 1
10+
11+
while l <= r:
12+
mid = (l+r)//2
13+
14+
# If our midpoint is greater than or equal to the starting point, and our target is greater than or equal to the
15+
# starting point, we know that we've landed in the correct side of the array relative to the target (the left in this case), so don't do
16+
# anything!
17+
if (nums[mid] >= nums[0]) and (target >= nums[0]):
18+
pass
19+
20+
# Conversely, if the mid is less than the start and the target is also less than the start, then we've landed on the correct side (the right side)
21+
# this is a strict less than comparision and not a less than or equal, because we can only be sure we're on the correct side
22+
# if the mid is strictly less than the start and so is the target. If we land on the left side and our target is on the right side, this could evaluate to true if it were <=.
23+
elif (nums[mid] < nums[0]) and (target < nums[0]):
24+
pass
25+
26+
# If we're on the wrong side, check which wrong side it is. If the start is greater than the target, we know we landed
27+
# landed on the left side and our target is on the right, so set the current value to be -INF. And vice versa for if the start is
28+
# less than the target.
29+
elif nums[0] > target:
30+
nums[mid] = float('-inf')
31+
else:
32+
nums[mid] = float('inf')
33+
34+
# Do a regular binary search now, since we've updated mid!
35+
if nums[mid] == target: return mid
36+
elif nums[mid] > target:
37+
r = mid - 1
38+
else:
39+
l = mid + 1
40+
return -1

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,21 @@
5757
| Preorder Traversal | 3 | - | Easy | Tree traversal | [Python](Python/BST_Preorder_Traversal.py) |
5858
| Postorder Traversal | 3 | - | Medium | Tree traversal | [Python](Python/BST_Postorder_Traversal.py) |
5959
| Inorder Successor | 4 | 285 | Medium | Tree traversal | [Python](Python/BST_InOrder_Successor.py) |
60-
| Closest Binary Search Tree Value | 5 | Easy | 270 | Tree, BST Search | [Python](Python/Closest_Binary_Search_Tree_Value.py) |
61-
| Binary Tree Level Order Traversal | 6 | 102 | Medium | Tree | [Python](Python/Binary_Tree_Level_Order_Traversal.py) |
60+
| Closest Binary Search Tree Value | 5 | Easy | 270 | BST Search | [Python](Python/Closest_Binary_Search_Tree_Value.py) |
61+
| Binary Tree Level Order Traversal | 6 | 102 | Medium | BFS | [Python](Python/Binary_Tree_Level_Order_Traversal.py) |
6262
| Validate Binary Search Tree | 7 | 98 | Medium | DFS, Recursion | [Python](Python/Validate_Binary_Tree.py) |
63-
| Kth Smallest Element in a BST | 8 | 230 | Medium | Tree | [Python](Python/Kth_Smallest_Element_in_a_BST.py) |
64-
| Same Tree | 9 | 100 | Easy | Tree | [Python](Python/Same_Tree.py) |
63+
| Same Tree | 9 | 100 | Easy | Recursion | [Python](Python/Same_Tree.py) |
6564
| Invert Binary Tree | 10 | 226 | Easy | Recursion | [Python](Python/Invert_Binary_Tree.py) |
6665
| Symmetric Tree | 11 | 101 | Easy | Tree | [Python](Python/Symmetric_Tree.py) |
6766
| Univalued Binary Tree | 12 | 965 | Easy | Tree | [Python](Python/BST_Univalue.py) |
6867
| Kth Smallest Element in a BST | 13 | 230 | Medium | Tree | [Python](Python/BST_Kth_Smallest_Element.py) |
6968
| Path Sum | 14 | 112 | Easy | DFS, Recursion | [Python](Python/Has_Path_Sum.py) |
7069
| Minimum Depth of Binary Tree | 15 | 111 | Easy | Tree | [Python](Python/BST_Min_Depth.py) |
7170
| Sum Root to Leaf Numbers | 16 | 129 | Medium | DFS, Recursion | [Python](Python/Sum_Root_to_Leaf_Numbers.py) |
72-
| Flatten Binary Tree to Linked List | 17 | 114 | Medium | Tree | [Python](Python/BT_Flatten_To.py) |
73-
| Convert BST to Greater Tree | 18 | 538 | Easy | Tree | [Python](Python/BST_To_Greater_Tree.py) |
71+
| Flatten Binary Tree to Linked List | 17 | 114 | Medium | Recursion | [Python](Python/BT_Flatten_To.py) |
72+
| Convert BST to Greater Tree | 18 | 538 | Easy | BFS | [Python](Python/BST_To_Greater_Tree.py) |
7473
| Binary Tree Zigzag Level Order Traversal | 19 | 103 | Medium | BFS | [Python](Python/Binary_Tree_Zig_Zag_Traversal.py) |
75-
| Populating Next Right Pointers in Each Node | 20 | 116 | Medium | Tree | [Python](Python/Connect_Next_Pointer_Tree.py) |
74+
| Populating Next Right Pointers in Each Node | 20 | 116 | Medium | BFS | [Python](Python/Connect_Next_Pointer_Tree.py) |
7675
| Binary Tree Maximum Path Sum | 21 | 124 | Hard | DFS, Recursion | [Python](Python/Binary_Tree_Maximum_Path_Sum.py) |
7776

7877

@@ -83,7 +82,7 @@
8382
| Merge Linked List | 2 | - | Easy | Linked List, Recursion | [Python](Python/Merge_Linked_List.py) |
8483
| Add Two Numbers | 3 | 2 | Medium | Linked List | [Python](Python/Add_Two_Numbers.py) |
8584
| Delete Node From Linked List | 4 | 237 | Easy | Linked List | [Python](Python/Delete_Node_From_Linked_List.py) |
86-
| Merge k Sorted Lists | 5 | [23](https://leetcode.com/problems/merge-k-sorted-lists/) | Hard | Linked List | [Python](Python/Merge_k_Sorted_Lists.py) |
85+
| Merge k Sorted Lists | 5 | [23](https://leetcode.com/problems/merge-k-sorted-lists/) | Hard | Linked List, Heap | [Python](Python/Merge_k_Sorted_Lists.py) |
8786

8887

8988

@@ -116,7 +115,9 @@
116115
## 🔍Searching
117116
| Title | Reccomended Order # | Leetcode # | Difficulty | Tags | Solution |
118117
| --- | --- | --- | --- | --- | --- |
119-
| Find First and Last Position of Element in Sorted Array | 1 | 34 | Medium | Array, Binary Search | [Python](Python/Find_First_and_Last_Position_of_Element_in_Sorted_Array.py) |
118+
| Find First and Last Position of Element in Sorted Array | 1 | 34 | Medium | Binary Search | [Python](Python/Find_First_and_Last_Position_of_Element_in_Sorted_Array.py) |
119+
| Search in Rotated Sorted Array | 2 | [33](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium | Binary Search | [Python](Python/Search_in_Rotated_Sorted_Array.py) |
120+
120121

121122

122123
## ⚒️ Divide and Conquer

0 commit comments

Comments
 (0)