📘
Find Peak Element
EasySearch & Sort
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:
Input:
1,2,3,1
Output:
2
Reasoning:
- The input array is
nums = [1, 2, 3, 1], and we need to find a peak element, which is an element strictly greater than its neighbors. - We compare each element with its neighbors:
nums[0] = 1is not greater thannums[1] = 2,nums[1] = 2is not greater thannums[2] = 3,nums[2] = 3is greater than bothnums[1] = 2andnums[3] = 1.
- Since
nums[2] = 3is a peak element, we return its index, which is 2. - The final output is the index of the peak element, which is 2.
Constraints:
- 1 <= len(nums) <= 1000
- -2^31 <= nums[i] <= 2^31 - 1
- nums[i] != nums[i + 1] for all valid i
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.