Suppose a sorted array is rotated at some 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.
You may assume no duplicate exists in the array.
This is actually the leftmost endpoint of the right part after the flip. A typical binary search variant is discussed in two ways:
1) if the search range is in the flipped part, it is a little more complicated:
1.1) if the center point is on the left side of the flip, you should look up to the right, because if there are no redundant elements, the minimum value cannot appear on the left side;
1.2) if the center is on the right side of the flip, you should search for it on the left. However, the elements corresponding to the current intermediate point should be cached at this time, because if you move to the left, the left part may be flipped, and the global minimum value may be missed.
2) If the search range is in the sorting part, the current minimum value must be at the leftmost end. However, at this time, the leftmost value is not necessarily the global minimum, and must be compared with the minimum value of the cache.
public int findMin(int[] num) {int low = 0, high = num.length - 1;int ret = Integer.MAX_VALUE;while (low <= high) {int mid = low + (high - low) / 2;// On the sorted part.if (num[low] <= num[high]) {return Math.min(num[low], ret);} else { // On the rotated part.// num[mid] is on the left rotated part.if (num[mid] >= num[low]) {low = mid + 1;} else { // num[mid] is on the right rotated part.ret = num[mid];high = mid - 1;}}} return ret;}
Note that the final return statement is actually not possible, because once low and high are equal, num [low] And num [High] must be equal and return directly.
Follow up for "find minimum in rotated sorted array ":
What if duplicates are allowed?
Wocould this affect the run-time complexity? How and why?
Suppose a sorted array is rotated at some 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 in duplicates.
If redundant elements exist, we must separately discuss the case where num [low] And num [High] are equal. In this case, you can only perform linear search honestly. If binary search is used, a problem may occur: If the leftmost and rightmost elements of the input array are the same, and there is a continuous and equal area, the center point may also be in the leftmost or rightmost consecutive equal area. At this time, it is impossible to determine which direction to search again. For example, [3, 3, 3, 3, 3, 4, 2, 3, 3] and [3, 3, 4, 2, 3, 3, 3, 3, 3], After retrieving the intermediate point, you cannot determine which direction to continue searching.
public int findMin(int[] num) {int low = 0, high = num.length - 1;int ret = Integer.MAX_VALUE;while (low <= high) {// Linear scan to find a non-flat part.if (num[low] == num[high]) {ret = Math.min(num[low], ret);low++;continue;}int mid = low + (high - low) / 2;// On the sorted part.if (num[low] < num[high]) {return Math.min(num[low], ret);} else { // On the rotated part.// num[mid] is on the left rotated part.if (num[mid] >= num[low]) {low = mid + 1;} else { // num[mid] is on the right rotated part.ret = num[mid];high = mid - 1;}}}return ret;}
Compared with the solution that does not consider redundant elements, an if statement is added to process the same conditions as num [low] And num [High] separately.
[Leetcode] Find minimum in rotated sorted Array