Easy understanding 68ms python O(logN) solution


  • 0
    E
    class Solution:
    # @param nums, an integer[]
    # @return an integer
    def findPeakElement(self, nums):
        b = 0
        e = len(nums) - 1
    
        while b <= e:
            mid = (b+e) / 2
    
            if b == e:
                return b
    
            if b+1 == e:
                return b if nums[b] > nums[e] else e
    
            if nums[mid-1] < nums[mid] > nums[mid+1]:
                return mid
            elif nums[mid-1] < nums[mid] < nums[mid+1]:
                b = mid
            else:
                e = mid
    
        return mid

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.