154. Find Minimum in Rotated Sorted Array II
文章目录
**Description**Hints**Submissions**Solutions
Total Accepted: 77016 Total Submissions: 209191 Difficulty: Hard Contributor: LeetCode
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.
Hide Tags Array Binary Search Hide Similar Problems (M) Find Minimum in Rotated Sorted Array
**解题思路 **
Binary Search
The minimum element must satisfy one of two conditions: 1) If rotate, A[min] < A[min - 1]; 2) If not, A[0].
Therefore, we can use binary search:
check the middle element, if it is less than previous one, then it is minimum.
If not, there are 2 conditions as well:
If it is greater than both left and right element, then minimum element should be on its right,
otherwise if it is less than right element, then minimum element should be on its left.
** when num[mid] == num[right], we couldn’t sure the position of minimum in mid’s left or right, so just let right reduce one. **
|
|
文章作者 Hustbill
上次更新 2017-06-11