Find Peak Element
Medium · binary search
You are given a list nums of integers. A peak is a position whose value is strictly larger than the value on each side of it. The first position only has a neighbour on its right, and the last position only has a neighbour on its left, so for those just that one neighbour has to be smaller. A list with a single item has a peak at index 0.
No two neighbouring values are equal, and the list is guaranteed to contain exactly one peak.
Return the index of that peak.
Examples
Example 1
Input: nums = [1, 4, 9, 6, 2]
Output: 2
9 is larger than both of its neighbours, 4 and 6. No other position is a peak.
Example 2
Input: nums = [8, 5, 3, 1]
Output: 0
The first position only needs to beat its right neighbour: 8 > 5.
Example 3
Input: nums = [-3, 0, 5, 7]
Output: 3
The last position only needs to beat its left neighbour: 7 > 5.
Constraints
- 1 <= len(nums) <= 10^4
- -10^9 <= nums[i] <= 10^9
- nums[i] != nums[i + 1] for every valid i.
- The list has exactly one peak.