Find Peak Element

Problem: Find Peak Element

Just imagine you are at middle of the array and trying to climb to a peak. What you need to do is just climb to a higher place you see.

Code in Python:

class Solution(object):
    def findPeakElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 2:
            return nums.index(max(nums))
        def findPeak(pos):
            if pos == 0 or pos == len(nums)-1:
                return pos
            if nums[pos] > nums[pos-1] and nums[pos] > nums[pos+1]:
                return pos
            if nums[pos] > nums[pos-1] and nums[pos] < nums[pos+1]:
                return findPeak(pos+1)
            return findPeak(pos-1)
        return findPeak(len(nums)/2)

results matching ""

    No results matching ""