Find Peak Element
A peak element is an element that is strictly greater than its neighbors. Given an integer array nums, find a peak element and return its index.
You may assume nums[-1] = nums[n] = -infinity. If there are multiple peaks, return the index of any one.
Example
1,2,3,1
2
- The input
numsholds integers, , at indices to . Treat each end as bordered by , so edge elements also have two neighbours to beat. - Writing for the value at index , a peak needs , strict on both sides.
- Index : , so the value loses to its right neighbour. Index : , so it loses too. Index : , so it loses to its left neighbour.
- Index : , so the value beats both neighbours. It is the only peak here, and the output is its index
2, not the value . - Notice that a peak always exists: the values rise from to and then fall, and since adjacent values are never equal, the place where the rise turns into a fall must be a peak. With the borders, even a strictly increasing list has one: its last element.
Constraints
- 1 <= len(nums) <= 1000
- -2^31 <= nums[i] <= 2^31 - 1
- nums[i] != nums[i + 1] for all valid i