Topic:
A peak element is an element, which is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1] , the find a peak element and return its index.
The array may be contain multiple peaks, in this case return the index to any one of the peaks is fine.
May imagine num[-1] = num[n] = -∞ .
For example, in array [1, 2, 3, 1] , 3 is a peak element and your function should return the index number 2.
Ideas:
According to test instructions, Num[0] is greater than the left non-existent element, num[size-1] is greater than the right of the non-existent element, if not present, then there will be num[0]<num[1],num[1]<num[2], is the order, num[ Size-2]<num[size-1], so num[size-1] is peak elem, so there must be. So this is the idea, Num[null] < num[0], we assume that the left element is smaller than the right element, then the first left element greater than the right one must be peak elem. such as Num[0].
/**/varfunction(nums ) {var n=nums.length; for (var i=1;i<n;i++) { if(nums[i]<nums[i-1]) { return i-1; } } return n-1;};
"Array" Find Peak Element