• 36.5%

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/#/description

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 become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

When num[mid] == num[hi], we couldn’t sure the position of minimum in mid’s left or right, so just let upper bound reduce one.

https://discuss.leetcode.com/topic/5182/rough-sketch-of-proof-why-o-lg-n-is-impossible

Rough sketch of proof why O(lg N) is impossible

Refer to this image: array config

Big version here: http://postimg.org/image/asbbeo2c9/

There are generally 3 types of array config, assuming sorted from smallest to biggest. x-axis is the array index, y-axis is the element value.

For non-duplicate case, you may use num[mid] > num[end] to distinguish (1)&(2) from (3), and thus safely eliminate half of the array, at each iteration.

For dup case, it is not as straightforward (impossible?) to distinguish between (1), (2) and (3), and hence eliminate half of the array by doing O(1) comparison, at each iteration.

8ms, 20.55%, June.20th, 2016

https://leetcode.com/discuss/19746/my-pretty-simple-code-to-solve-it

My pretty simple code to solve it

When num[mid] == num[hi], we couldn’t sure the position of minimum in mid’s left or right, so just let upper bound reduce one.

https://discuss.leetcode.com/topic/4253/one-simple-and-clear-method-with-o-1-space-and-worst-o-n-time

One simple and clear method with O(1) space and worst O(n) time

https://discuss.leetcode.com/topic/4264/my-c-solution-24ms-is-there-any-better-solution

My C++ solution 24ms is there any better solution ?

https://discuss.leetcode.com/topic/19165/8ms-13-lines-c-solution

8ms 13-lines C++ Solution

This problem is more or less the same as Find Minimum in Rotated Sorted Array. And one key difference is as stated in the solution tag. That is, due to duplicates, we may not be able to throw one half sometimes. And in this case, we could just apply linear search and the time complexity will become O(n).

The idea to solve this problem is still to use invariants. We set l to be the left pointer and r to be the right pointer. Since duplicates exist, the invatiant is nums[l] >= nums[r] (if it does not hold, then nums[l] will simply be the minimum). We then begin binary search by comparing nums[l], nums[r] with nums[mid].

1. If nums[l] = nums[r] = nums[mid], simply apply linear search within nums[l..r].
2. If nums[mid] <= nums[r], then the mininum cannot appear right to mid, so set r = mid;
3. If nums[mid] > nums[r], then mid is in the first larger half and r is in the second smaller half, so the minimum is to the right of mid: set l = mid + 1.

The code is as follows.

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

Python solution. Worst case O(N)

Solution Mime:

64ms, 20.87%, June.20th, 2016

Solution Mime:

52ms, 65.53%, June.20th, 2016

1ms, 6.02%, June.20th, 2016

https://leetcode.com/discuss/60147/super-simple-and-clean-java-binary-search