http://www.lintcode.com/en/problem/find-minimum-in-rotated-sorted-array/\#

we need to compare the cut position value with the largest of the second part of the array. (not the first value because the rotated sorted array could be rotated 0 times)

public class Solution {
    /**
     * @param nums: a rotated sorted array
     * @return: the minimum number in the array
     */
    public int findMin(int[] nums) {
        // write your code here
        if (nums == null) {
            return 0;
        }

        int pivot = nums[nums.length - 1];
        int start = 0;
        int end = nums.length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] < pivot) {
                end = mid;
            } else {
                start = mid;
            }
        }

        if (nums[start] <= nums[end]) {
            return nums[start];
        }

        return nums[end];
    }
}

results matching ""

    No results matching ""