- refer to mit peak finding lecture 27.40
- our goal to narrow down the array and only one element left
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;
}
}