Python Solution using Binary search, complexity log(n)


  • 0
    P
    class Solution(object):
        def findPeakElement(self, nums):
          """
          :type nums: List[int]
          :rtype: int
          """
          low = 0
          end = len(nums) -1
          mid = (low+end)/2
          while end > low:
             if mid!=low and nums[mid] < nums[mid -1] :
                 end = mid -1
             elif nums[mid+1]>nums[mid]:
                 low = mid + 1
             else:
                 return mid
             mid = (low+end)/2
          return mid
    

Log in to reply
 

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