Medium
LABPeak Index in a Mountain Array
Given a mountain array, return the index of the peak value.
EXAMPLES
Example 1
Input
{
"arr": [
0,
2,
1,
0
]
}
Output
1FUNCTION SHAPE
arr: intArray→intSOLUTION NOTE
Same peak-finding template as Find Peak Element, but the mountain-array guarantee means the peak is unique.
Reveal reference solution +
pythonREFERENCE
def peakIndexInMountainArray(self, arr: List[int]) -> int:
left, right = 0, len(arr) - 1
while left < right:
mid = ceil(left + (right - left) / 2)
if arr[mid - 1] <= arr[mid]:
left = mid
else:
right = mid - 1
return leftTime
O(log n)Space
O(1)