less than 1 minute read

852. Peak Index in a Mountain Array (medium)

O(n) solution

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        n = len(arr)
        for i in range(n - 2):
            a, b, c = arr[i], arr[i + 1], arr[i + 2]
            if a < b and b > c:
                return i + 1
        return -1

O(log n) Solution

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        i = 0
        j = len(arr) - 1
        while i < j:
            mid = (i + j) // 2

            if arr[mid] < arr[mid + 1]:
                i = mid + 1
            else:
                j = mid

        return i