154. Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array may contain duplicates.
This is the follow-up of the previous problem. The condition is that the array contains duplicates. This condition will break some of our assumptions made in the previous solution.
The change is that when you have duplicates on the side you cannot divide the array into half to find the minimal. E.g. 3133, in this case the medium 3 equals to both first and entry entry. There is no way you can guess whether 1 is on the left side or right side.
The solution is recursively following steps:
If the array has less than 3 entries, find the minimal one using Min.
If the medium is bigger than the last entry, we are for sure that the minimal value is on the right side of the array.
If the medium is smaller than the first entry we can assure that the minimal value is on the left side of the array.
For equal cases we need to check both sides and find the minimal value.
You can find my code here.
https://gist.github.com/zyzyis/1ae909e64f3597307246













