-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0065.py
30 lines (26 loc) · 1.08 KB
/
LC0065.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
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
left, right = 0, n - 1
# Find the index of the pivot element (the smallest element)
while left <= right:
mid = (left + right) // 2
if nums[mid] > nums[-1]:
left = mid + 1
else:
right = mid - 1
# Shift elements in circular manner, with the pivot element at index 0.
# Then perform a regular binary search
def shiftedBinarySearch(pivot_index, target):
shift = n - pivot_index
left, right = (pivot_index + shift) % n, (pivot_index - 1 + shift) % n
while left <= right:
mid = (left + right) // 2
if nums[(mid - shift) % n] == target:
return (mid - shift) % n
elif nums[(mid - shift) % n] > target:
right = mid - 1
else:
left = mid + 1
return -1
return shiftedBinarySearch(left, target)