Loading...
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.
1,2,3,1
2
nums = [1, 2, 3, 1], and we need to find a peak element, which is an element strictly greater than its neighbors.nums[0] = 1 is not greater than nums[1] = 2,nums[1] = 2 is not greater than nums[2] = 3,nums[2] = 3 is greater than both nums[1] = 2 and nums[3] = 1.nums[2] = 3 is a peak element, we return its index, which is 2.