My C solution (2ms). Could anyone re-check my solution?


  • 0
    B

    I have little confidence about my solution although it has passed the test.

    Could anyone re-check my solution?

    int findMin(int num[], int n) {
        return binarySearch(num, n, 0, n - 1);
    }
    
    int binarySearch(int num[], int n, int low, int high) {
        if (num[low] <= num[high]) return num[low];    //if num[low] <= num[high], this sub-array is increasing.
        else if (high - low == 1) return num[high];
        
        int m = (low + high) / 2;
        if (num[m] > num[low]) {
            // left is increasing
            if (num[low] > num[high])   // the pivot is in the right sub-array.
                return binarySearch(num, n, m, high);
            else // if num[m] > num[low] and num[low] <= num[high], this means the array is not rotated
                return num[low];
        } else if (num[m] < num[high]) {
            // right is increasing
            if (num[low] > num[high]) return binarySearch(num, n, low, m);
            else return num[low];
        }
    }

Log in to reply
 

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