I am trying to find the minimum in a rotated sorted array with duplicates:
I tried this:
def find_pivot(arr):
lo = 0
hi = len(arr) -1
while lo<=hi:
mid = (hi+lo)//2
if arr[mid]<arr[0]:
hi = mid-1
else:
lo = mid+1
return lo
class Solution:
"""
@param nums: a rotated sorted array
@return: the minimum number in the array
"""
def findMin(self, nums):
pivot_index = find_pivot(nums)
left = nums[:pivot_index]
right = nums[pivot_index:]
if right:
return right[0]
return left[0]
This fails when the array is [999,999,1000,1000,10000,0,999,999,999]. My algorithm returns 999, when it should return 0
How do I fix this?