in LintCode Binary Search ~ read.

Find Minimum in Rotated Sorted Array II

LintCode-160.Find Minimum in Rotated Sorted Array II

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.

Notice

The array may contain duplicates.

Example

Given [4,4,5,6,7,0,1,2] return 0.

public class Solution {  
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] num) {
        if (num == null || num.length == 0) {
            return -1;
        }

        int low = 0, high = num.length - 1;

        if (num[low] < num[high]) {
            return num[low];
        }

        while (low + 1 < high) {
            int middle = low + (high - low) / 2;

            if (num[low] < num[middle]) {
                low = middle;
            } else if (num[low] > num[middle]) {
                high = middle;
            } else {
                low++;
            }
        }

        return Math.min(num[low], num[high]);
    }
}

Hope this helps,
Michael