Find Peak Element
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.
Click to show spoilers.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
The problem is to find the maximum value of the array in a disguised way.
Two-point search, Time complexity O (n).
1 classSolution {2 Public:3 intFindpeakelement (vector<int>&nums) {4 intleft=0, Right=nums.size ()-1;5 while(left<Right )6 {7 intMid = (left+right)/2;8 if(nums[mid]<nums[mid+1])9 {Tenleft = mid+1; One } A Else - { -right =mid; the } - } - returnLeft ; - } +};
[Leetcode] Find Peak Element