-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.py
More file actions
114 lines (83 loc) · 3 KB
/
33.py
File metadata and controls
114 lines (83 loc) · 3 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
def findPivot(nums, inpL, inpR):
## Find the smallest number in the array
l, r = inpL, inpR
midpoint = (l + r) // 2
# tempMid = int((l+r)/2)
# print(midpoint, tempMid)
# print(l, r)
if inpR < inpL:
return -1
elif inpR == inpL:
return inpR
else:
# if len(nums) == 1:
# return 0
# if len(nums) == 2:
# if nums[0] < nums[1]:
# return 0
# return 1
if midpoint < r and nums[midpoint] > nums[midpoint+1]:
return midpoint
if midpoint > l and nums[midpoint] < nums[midpoint-1]:
return midpoint-1
if nums[l] >= nums[midpoint] :
return findPivot(nums, l, midpoint-1)
return findPivot(nums, midpoint + 1, r)
def binarySearch(nums, target):
## Find the index of target and return it
targetIndex = -1
l, r = 0, len(nums)
if len(nums) == 0:
return targetIndex
elif len(nums) == 1:
if (nums[0] == target):
targetIndex = 0
elif len(nums) == 2:
if (nums[0] == target):
targetIndex = 0
if (nums[1] == target):
targetIndex = 1
else:
midpoint = (l + r -1) // 2
if target == nums[midpoint]:
return midpoint
else:
if target < nums[midpoint]:
return binarySearch(nums[l:midpoint], target)
else:
temp = binarySearch(nums[midpoint:], target)
if temp == -1:
return -1
else:
targetIndex = midpoint + temp
return targetIndex
pivotIndex = findPivot(nums, 0, len(nums)-1)
# print(pivotIndex)
leftList, rightList = nums[0:pivotIndex+1], nums[pivotIndex+1:]
# print(leftList, rightList)
leftIndex = binarySearch(leftList, target)
rightIndex = binarySearch(rightList, target)
# print(leftIndex, rightIndex, pivotIndex)
if leftIndex == -1 and rightIndex == -1:
return -1
else:
if rightIndex != -1:
if leftIndex == -1:
return pivotIndex + rightIndex + 1
return leftIndex
return -1
s = Solution()
# inpList = [4,5,6,7,0,1,2,3]
# inpList = [4,5,6,7,0,1,2,3]
inpList = [3,4,5,6,1,2]
# print(s.search(inpList, 3))
for i in inpList:
print(s.search(inpList, i))
# print(s.search(inpList, 3))