Share my accepted C++ solution using recursion


  • 0
    S
    class Solution {
    public:
        int findMin(vector<int> &num) {
            int n = num.size(); 
            if (n <1) 
                return 0;
            return bSearch(num, 0, n-1);
        }
        
        int bSearch(const vector<int> &num, int beg, int end) {
            if (beg == end)
                return num[beg];
            if (beg == end - 1)
                return min(num[beg], num[end]);
            
            int mid = beg + (end - beg)/2;
            
            if (num[mid] == num[beg] && num[mid] == num[end])
                return bSearch(num, beg+1, end);
            if (num[mid] < num[end] && num[mid] > num[beg])
                return num[beg];
            if (num[mid] <= num[end])
                return bSearch(num, beg, mid);
            if (num[mid] >= num[beg])
                return bSearch(num, mid, end);
        }
    };

Log in to reply
 

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