8ms AC solution


  • 0
    T
    class Solution {
    public:
        int f(vector<int>& n, int l, int r) {
            if (l == r) return n[l];
            int m = (r - l) / 2 + l;
            if (n[m] > n[l] && n[m] > n[r]) return f(n, m+1, r);
            else if (n[m] < n[l] && n[m] < n[r]) return f(n, l, m);
            else {
                int k1 = f(n, l, m);
                int k2 = f(n, m+1, r);
                return k1 < k2 ? k1 : k2;
            }
        }
    
        int findMin(vector<int>& nums) {
            int sz = nums.size();
            if (sz == 1) return nums[0];
            return f(nums, 0, sz-1);
        }
    };
    
    
    

Log in to reply
 

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