LeetCode: Find Peak Element Posted on February 27, 2018July 26, 2020 by braindenny Find Peak Element Similar Problems: CheatSheet: Leetcode For Code Interview CheatSheet: Common Code Problems & Follow-ups Tag: #array, #binarysearch A peak element is an element that is greater than its neighbors. Given an input array where num[i] != num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = negative infinity. For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2. Note: Your solution should be in logarithmic complexity. Github: code.dennyzhang.com Credits To: leetcode.com Leave me comments, if you have better ways to solve. ## https://code.dennyzhang.com/find-peak-element class Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int """ length = len(nums) if length == 1: return 0 if length == 0: return -1 for i in range(1, length-1): if nums[i] > nums[i-1] and nums[i] > nums[i+1]: return i if nums[0] > nums[1]: return 0 if nums[length-1] > nums[length-2]: return length-1 return -1 Post Views: 3