My java solution (logN)


  • 4
    2
    public int findMin(int[] num) {
        int i = 0;
        int j = num.length - 1;
        
        int res = 0;
        
        while (j >= i) {
            
            if (num[i] <= num[j]) {
                res = num[i];
                break;
            }
            
            int m = i + (j - i) / 2;
            
            if (num[i] <= num[m]) {
                i = m + 1;
            }
            else {
                j = m;
            }
        }
        
        return res;
    }
    

    Just use binary search. When found the subarray is from low to high, simply return the first value.

    If the length of the array is 0, the return value is 0 or we can set any value we want to res.


  • 0
    Y
    This post is deleted!

  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example


  • 0
    Y

    I compare num[i] with num[j] before while loop.
    but same idea anyway


  • 0
    S

    Isn't this wrong, because if the input is int[] array = {5,4,10,9,8,7,6}, the answer returned is 5. When it should be 4.


  • 0
    2

    please read the question carefully


  • 0
    S

    Not sure what I am missing, rotated sorted array.


Log in to reply
 

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