My concise accepted solution - binary search with explanation


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

    This is a question to find upper bound, namely to find the first element nums[i] > nums[i + 1] from left-end.

    Learn more


Log in to reply
 

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