↓Skip to main content
  1. LeetCode/

LeetCode 162: Find Peak Element

·2 mins· ·
LeetCode Medium Array Binary-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-11

  • Submissions: Runtime Error → Wrong Answer → Runtime Error → Accepted → Runtime Error → Accepted ×2 → Wrong Answer → Accepted

Approach
#

Treat both outsides as negative infinity (Binary Search). Look at mid and mid + 1:

  • nums[mid] < nums[mid + 1]: uphill to the right, so a peak exists there (if it rises all the way, the last element is a peak), l = mid + 1.
  • Otherwise it is downhill from mid, so a peak exists at or left of mid, r = mid.

Neighbors are never equal, so there are no flat stretches.

My Attempts
#

The early versions compared both mid - 1 and mid + 1, repeatedly going out of range (IndexError); one typed num for nums; the last WA returned a non-peak index, e.g. not 2 for [0, 2, 4].

Solution
#

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        l, r = 0, len(nums) - 1
        while l < r:
            mid = (l + r) // 2
            if nums[mid] < nums[mid + 1]:
                l = mid + 1
            else:
                r = mid
        return l

Complexity
#

Time O(log n), space O(1).

Mistakes to Avoid
#

  • With while l < r, mid + 1 is always in range.

Interview Explanation
#

I compare the middle with its right neighbor: uphill means a peak lies to the right, otherwise at or left of the middle. When the interval shrinks to one index, that is a peak. Time O(log n).

Related

LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 153: Find Minimum in Rotated Sorted Array
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 153 note: compare mid with the right end: nums[mid] > nums[r] puts the minimum to the right, otherwise mid may be it, so r = mid
LeetCode 875: Koko Eating Bananas
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 875 note: binary search the speed for the smallest k with total hours <= h, continuing left even when the hours equal h
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based
LeetCode 81: Search in Rotated Sorted Array II
·2 mins
LeetCode Medium Array Binary-Search
LeetCode 81 note: with duplicates, nums[l] == nums[mid] hides which half is sorted, so just move l right by one; worst case O(n)
LeetCode 74: Search a 2D Matrix
·2 mins
LeetCode Medium Array Binary-Search Matrix
LeetCode 74 note: each row continues the previous one in sorted order, so binary search it as a 1-D array of length m × n, mapping mid to (mid // n, mid % n)