-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_in_rotated_sorted_array_33.py
47 lines (38 loc) · 1.64 KB
/
search_in_rotated_sorted_array_33.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution:
def search(self, nums: List[int], target: int) -> int:
# Must be O(logN)
left = 0
right = len(nums) -1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
# First I wanna determine what half of the array is sorted
# first let's check and see if its the left half:
if nums[left] == target:
return left
elif nums[right] == target:
return right
if nums[left] < nums[mid]:
#then left is sorted
# is it possible for our target to be in this range?
if nums[left] <= target < nums[mid]:
# since I did an inclusive search, check if nums[left] is target
if nums[left] == target:
return left
#yes, search the left half
right = mid -1
else:
#no, we gotta check the right half (next time)
left = mid + 1
else:
#then the right half is sorted
# is it possible for target to be in that range?
if nums[mid] < target <= nums[right]:
# since I did an inclusive check, we gotta see if nums[right]
if nums[right] == target:
return right
left = mid + 1
else:
right = mid -1
return -1