package leetcode;
/**
* Find Minimum in Rotated Sorted Array
*
* <pre>
* 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.
*
* You may assume no duplicate exists in the array.
* </pre>
* */
public class FindMinimuminRotatedSortedArray {
public class Solution {
public int findMin(int[] num) {
return _findMin(num, 0, num.length - 1);
}
private int _findMin(int[] num, int start, int end) {
if (num[start] < num[end]) {
return num[start];
}
if (start == end) {
return num[end];
}
if (start + 1 == end) {
return num[end];
}
int mid = (end + start) / 2;
if (num[mid] < num[start]) {
return _findMin(num, start, mid);
} else {
return _findMin(num, mid, end);
}
}
}
}
/**
* Find Minimum in Rotated Sorted Array
*
* <pre>
* 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.
*
* You may assume no duplicate exists in the array.
* </pre>
* */
public class FindMinimuminRotatedSortedArray {
public class Solution {
public int findMin(int[] num) {
return _findMin(num, 0, num.length - 1);
}
private int _findMin(int[] num, int start, int end) {
if (num[start] < num[end]) {
return num[start];
}
if (start == end) {
return num[end];
}
if (start + 1 == end) {
return num[end];
}
int mid = (end + start) / 2;
if (num[mid] < num[start]) {
return _findMin(num, start, mid);
} else {
return _findMin(num, mid, end);
}
}
}
}