Java solution 0ms, binary search


  • 0
    M
    public int findPeakElement(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] < nums[mid + 1]) {
                start = mid;
            } else {
            // From question that given an input array where num[i] ≠ num[i+1]
                end = mid;
            }
        }
        if (nums[start] > nums[end]) {
            return start;
        }
        return end;
    }

Log in to reply
 

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