Simplest and easy to understand solution :D


  • 0
    N
    class Solution {
    public:
        int findMin(vector<int>& a) {
            int n = a.size();
            int lo = 0,hi = n-1,ret = -1;
            while(lo<=hi){
                int mid = (lo+hi)>>1;
                int next = a[(mid+1)%n];
                int prev = a[(mid-1+n)%n];
                if(a[mid]<=prev and a[mid]<=next){
                    ret = a[mid];
                    break;
                }
                if(a[mid]>a[hi]){
                    lo = mid+1;
                }else{
                    hi = mid-1;
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

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