Java solution O(logN)


  • 0
    J

    public class Solution {

    int min;
    public int findMin(int[] nums) {
        min = nums[0];
        search(nums,0,nums.length);
        return min;
    }
    void search(int[] nums,int left,int right){
        if (left >= right) return;
        int mid = (left + right) /2;
        if (min > nums[mid]) {
            min = nums[mid];
            search(nums,left,mid);
        }
        else search(nums,mid+1,right);
    }
    

    }


  • 0
    Y

    I think you did not consider descending order. Just try the test case [2,1,0].


  • 0
    T

    [2,1,0] is not a valid input, because it's not a sorted (implicitly ascending if not said otherwise) array rotated. Any way you rotate you'll never get an ascending array: [1,0,2], [0,2,1].


Log in to reply
 

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