5 lines Java solution, very intuitive, O(n)


  • 0

    The idea is to find the pivot index, which splits the array in two sub arrays, before the pivot and after the pivot. Then the minimum is the smaller first element in the two sub-arrays.

    public int findMin(int[] nums) {
        if(nums.length == 1)
            return nums[0];
            
        int i = 0;
        while(nums[i] <= nums[++i] && i < nums.length - 1);
        return Math.min(nums[0], nums[i]);
    }

  • 0
    S

    If you already have the pivot index, you don't need to compare with first element. The pivot element is the smallest one because of this rotated sort array.


  • 0
    S

    if the array is in order than the first element maybe the pivot


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.