Skip to content

Latest commit

 

History

History
10 lines (9 loc) · 431 Bytes

File metadata and controls

10 lines (9 loc) · 431 Bytes

Solution

We can use binary search to find the peak in the array. We can assume that index of -1 and index of array.length is negative infinity. We only need to find one possible local peak. Thus, we can compare the mid with mid + 1.

if array[mid] < array[mid + 1]:
    left = mid + 1
else:
    right = mid

No need to do postprocessing because the target is guaranteed to exist.
time: O(logn)
space: O(1)