Find Peak Element
A peak element is an element that is strictly greater than its neighbors.
Given a 0-indexed integer array nums, find a peak element and return its index. If the array contains multiple peak elements, you may return the index of any one of them.
You may imagine that nums[-1] = -∞ and nums[n] = -∞, where n is the length of nums. In other words, elements outside the array are considered smaller than any array element.
Your solution must run in O(log n) time.
Example 1
Input
nums = [1,2,3,1]Output
2The element 3 at index 2 is greater than both of its neighbors, so it is a peak.
Example 2
Input
nums = [1,2,1,3,5,6,4]Output
5The element 6 at index 5 is greater than both of its neighbors, so it is a valid peak.
Constraints
- 1 <= nums.length <= 1000
- -2^31 <= nums[i] <= 2^31 - 1
- nums[i] != nums[i + 1] for all valid i