Find Minimum in Rotated Sorted Array
Description
Suppose a sorted array 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.
Notice
You may assume no duplicate exists in the array.
Example
Given[4, 5, 6, 7, 0, 1, 2]
return0
Related problems
Find Minimum in Rotated Sorted Array II
Search in Rotated Sorted Array II
Implementation
Link: http://lintcode.com/en/problem/strstr/
class Solution {
public:
/**
* @param num: a rotated sorted array
* @return: the minimum number in the array
*/
int findMin(vector<int> &num) {
// write your code here
if(num.size() == 0) return -1;
int start = 0, end = num.size() - 1;
while(start + 1 < end){
int mid = start + (end - start)/2;
if(num[mid] > num[end]){
start = mid;
}else{
end = mid;
}
}
if(num[start] <= num[end]) {
return num[start];
} else {
return num[end];
}
}
};