My AC c++ Code in O(lgn)


  • 1
    Y
    int findPeakElement(const vector<int> &num) {
            int len = num.size();
            if (len == 1) return 0;
            int left = 0, right = len - 1;
            int mid;
            while (left < right - 1) {
                mid = left + (right - left) / 2;
                if (num[mid] > num[mid-1] && num[mid] > num[mid+1]) return mid;
                if (num[mid] < num[mid-1]) right = mid;  // like hill climbing 
                else left = mid;
            }
            return num[left] > num[right] ? left : right;
        }

Log in to reply
 

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