Skip to content

Latest commit

 

History

History
27 lines (26 loc) · 810 Bytes

162. Find Peak Element.md

File metadata and controls

27 lines (26 loc) · 810 Bytes

binary search

class Solution {
    public int findPeakElement(int[] nums) {
        return recur(nums, 0, nums.length);
    }
    int recur(int[] nums, int l, int r){
        
        if(nums.length==1) return 0;
        while(l < r){
            int mid = (r-l)/2+l;
            // 
            if(mid-1 >=0 && nums[mid] < nums[mid-1]) {
                r = mid;
            }else if(mid+1 < nums.length && nums[mid] < nums[mid+1]){
                l = mid+1;
            }else {
                //only one element left
                return mid;
            }
        }
        return -1;
    }
}