← 返回 meta 的题目列表Find a Local Minimum (Valley) in an Array
类型:online_judge
Given an integer array nums of length n, return any index i such that nums[i] is strictly smaller than its adjacent neighbor(s) (a local minimum):
If 0 < i < n-1, then nums[i] < nums[i-1] and nums[i] < nums[i+1].
For boundary elements:
If i = 0, only require nums[0] < nums[1].
If i = n-1, only require nums[n-1] < nums[n-2].
Constraints/requirements:
Design an O(log n) algorithm (e.g., binary search).
If multiple answers exist, return any.
Constraints
2 <= n <= 2 * 10^5
nums[i] fits in 32-bit signed integer
The input guarantees at least one valid index
Example
Input: [9, 7, 8] Output: 1
Input: [3, 2, 1, 4] Output: 2
Implement: read the array and print one valid 0-based index.
Example
Input
9 7 8
Output
1