← 返回 meta 的题目列表Find Peak Element
类型:online_judge
Problem: Find Peak Element
Given an integer array nums, return the index of any peak element.
A peak element is defined as an element that is strictly greater than its neighbors, i.e.:
nums[i] > nums[i-1] (if i-1 exists)
nums[i] > nums[i+1] (if i+1 exists)
Assume the out-of-bounds neighbors are negative infinity:
nums[-1] = -∞
nums[n] = -∞
Requirements:
Your solution must run in O(log n) time.
If multiple peaks exist, returning any one index is acceptable.
Input (stdin)
One line of integers representing array nums separated by spaces.
Output (stdout)
Print one integer: the index of any peak element.
Constraints
1 <= n <= 2 * 10^5
-10^9 <= nums[i] <= 10^9
nums[i] != nums[i+1]
Examples
Input:
1 2 3 1
Output:
2
Input:
1 2 1 3 5 6 4
Output:
1
Input:
1
Output:
0
Input:
2 1
Output:
0
Input:
1 3 2
Output:
1
Example
Input
1 2 3 1
Output
2