Leetcode 162 Solution

This article provides solution to leetcode question 162 (find-peak-element)

https://leetcode.com/problems/find-peak-element

Solution

class Solution(object):
    def findPeakElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        l = 0
        r = len(nums) - 1
        while l <= r:
            m = (l + r) // 2

            left_greater = nums[m] > nums[m - 1] if m > 0 else True
            right_greater = nums[m] > nums[m + 1] if m < len(nums) - 1 else True

            if left_greater and right_greater:
                return m

            if not left_greater:
                r = m - 1
                continue

            if not right_greater:
                l = m + 1
                continue

        return 0