Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e.,0 1 2 4 5 6 7
might become4 5 6 7 0 1 2
).
Find the minimum element.
The array may contain duplicates.
寻找旋转有序重复数组的最小值是对Find Minimum in Rotated Sorted Array 的延伸,当数组中存在大量的重复数字时,就会破坏二分查找法的机制,我们无法取得O(lgn)的时间复杂度,又将会回到简单粗暴的O(n),比如如下两种情况:
{2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 1, 2} 和 {2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2}, 我们发现,当第一个数字和最后一个数字,还有中间那个数字全部相等的时候,二分查找法就崩溃了,因为它无法判断到底该去左半边还是右半边。这种情况下,我们将end减一,略过与mid相同的数字,然后对剩余的部分继续用二分查找法,在最坏的情况下,比如数组所有元素都相同,时间复杂度会升到O(n)
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
start, end = 0, len(nums)-1
while start + 1 < end:
mid = (start+end)/2
#if mid equals to end, that means it's fine to remove end,the smallest element won't be removed
if nums[mid] == nums[end]:
end -= 1
elif nums[mid]>nums[end]:
#minV = min(minV, nums[start])
start = mid
else:
#minV = min(minV, nums[mid])
end = mid
return min(nums[start], nums[end])
Last updated
Was this helpful?