Skip to contentKEEP THE CRAFTTHE SANCTUARY

Find a Peak Element

Return the index of any value that is bigger than both of its neighbors.

The problem

Implement Solution.findPeakElement(nums). nums holds between 1 and 1,000 integers, and no two neighbors are equal. A peak is a value strictly bigger than its neighbors; pretend the values just outside the list are negative infinity, so the first and last values only need to beat their one real neighbor. Return the index of any peak. Your solution must run in O(log n) time.

Starter code

class Solution:
    def findPeakElement(self, nums):
        pass

Public examples

  1. nums = [1, 2, 3, 1]gives2
  2. nums = [-3]gives0
  3. nums = [1, 3, 5, 7, 9, 6, 2]gives4

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.