-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSolution.py
43 lines (35 loc) · 1.12 KB
/
Solution.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
#
# Created on Sun Apr 19 2020
#
# Title: Leetcode - Search in Rotated Sorted Array
#
# Author: Vatsal Mistry
# Web: mistryvatsal.github.io
#
class Solution:
def binarySearch(self, nums, low, high, target):
if high >= low:
mid = (low + high) // 2
if target == nums[mid]:
return mid
if target < nums[mid]:
return self.binarySearch(nums, low, mid-1, target)
return self.binarySearch(nums, mid+1, high, target)
return -1
def findPivot(self, nums):
return nums.index(min(nums))
def search(self, nums, target):
if(len(nums) == 0): return -1
pivot = self.findPivot(nums)
low = 0
high = len(nums)-1
if pivot == 0:
return self.binarySearch(nums, low, high, target)
if target == nums[pivot]:
return pivot
if nums[0] <= target:
return self.binarySearch(nums, low, pivot-1, target)
return self.binarySearch(nums, pivot+1, high, target)
# Sample Input
obj = Solution()
print(obj.search([4, 6, 7, 8, 1, 3], 7))