Simple Java, O(log(n))


  • 0
    A
    public int findPeakElement(int[] nums) {
        int l = 0, h = nums.length;
        while (l < h) {
            int m = l + (h - l) / 2;
            if (nums[m] < (m+1 < nums.length ? nums[m+1] : Integer.MIN_VALUE ))
                l = m + 1;
            else if (nums[m] < (m-1 >= 0 ? nums[m-1] : Integer.MIN_VALUE))
                h = m;
            else return m;
        }
        return h;
    }

Log in to reply
 

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