134. Peak in the Terrain
Medium
You are given an integer array nums that strictly rises to a single peak and then strictly falls. For this problem, a missing neighbor at either edge has a value equivalent to negative infinity.
Return the index of the peak, meaning the index of an element that is greater than its neighbors. The array may contain just one element, which is then the peak.
Example 1
Input: nums = [1]
Output: 0
Explanation: The only element is the peak.
Example 2
Input: nums = [-7, -2, 4, 9, 3, -1]
Output: 3
Explanation: The value 9 is the peak, at index 3.
Example 3
Input: nums = [2, 6, 11]
Output: 2
Explanation: Since the array only rises, its last element is the peak.
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- nums strictly rises up to its single peak and strictly falls afterward.
Binary searchArrays
Saved in this browser
Caso 1
nums =
[1]
Expected =
0
Caso 2
nums =
[-7,-2,4,9,3,-1]
Expected =
3
Caso 3
nums =
[2,6,11]
Expected =
2
Run checks these 3 cases. Submit runs all 12, including hidden ones.