-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_rotated_array.py
More file actions
43 lines (34 loc) · 1.16 KB
/
Copy pathsearch_rotated_array.py
File metadata and controls
43 lines (34 loc) · 1.16 KB
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
import sys
# https://leetcode.com/problems/search-in-rotated-sorted-array/
def search(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
result = binarySearch(nums, 0, len(nums)-1, target)
return result
def binarySearch(arr, l, r, x):
if l > r:
return -1
m = int((l + r) / 2)
if arr[m] == x:
return m
if arr[m] < arr[r]: # right half is sorted
if x >= arr[m] and x <= arr[r]: # eligible, go right
return binarySearch(arr, m + 1, r, x)
else: # not eligible, go left
return binarySearch(arr, l, m - 1, x)
elif x >= arr[l] and x <= arr[m]: # left half is sorted
return binarySearch(arr, l, m - 1, x) # eligible, go left
else:
return binarySearch(arr, m + 1, r, x) # not eligible, go right (unsorted)
def main():
str = input('list: ')
nums = list(map(int, str.split(',')))
str = input('target: ')
target = int(str)
output = search(nums, target)
print('output: ', output)
if __name__ == '__main__':
main()