-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm81.py
48 lines (39 loc) · 1.32 KB
/
m81.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
47
48
# adapted from my search in sorted array i answer
class Solution:
def search(self, nums: List[int], target: int) -> bool:
n = len(nums)
# find rotation amount via bin search
left, right = 0, n - 1
while left < right - 1 :
# if same vals
while left < right - 1 and left < n - 1 and nums[left] == nums[left + 1] :
left += 1
while left < right - 1 and right > 1 and nums[right] == nums[right - 1] :
right -= 1
# get mid index and val
mid = (left + right) // 2
val = nums[mid]
# ret if found
if target == val :
return True
# shift
if val < nums[left] :
right = mid
continue
left = mid
# account for no rotation
offset = 0 if nums[right] - nums[left] > 0 else right
# regular bin search
left, right = offset, offset + n - 1
while left <= right:
mid = ((left + right) // 2)
val = nums[mid % n]
# found val
if val == target :
return True
# shift
if target < val :
right = mid - 1
continue
left = mid + 1
return False