Find peak element

November 10, 2022

binary-search

Problem URL: Find peak element

We will use binary search to find the peak element. We will then return the index of the peak element.

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        start, end = 0, len(nums)-1
        while start < end:
            mid = start + (end-start) // 2
            if nums[mid] > nums[mid+1]:
                end = mid
            else:
                start = mid+1
        return start

Time Complexity: O(log(n))
Space Complexity: O(1)