Problem 334480 · medium · Level 03 Linear Management & Searching

Summit of a Mountain List

binary search · peak finding

A mountain list strictly increases up to a single summit and then strictly decreases. Given such a list heights (length at least 3), return the index of the summit.

Examples

Input:  heights = [1, 3, 5, 4, 2]
Output: 2

Input:  heights = [0, 10, 3]
Output: 1

Input:  heights = [1, 2, 3, 4, 3]
Output: 3

Constraints

  • 3 <= len(heights) <= 10**6
  • Adjacent heights are never equal, and the list is a valid mountain.
  • O(log n) time; finding the maximum with a full scan is too slow.

Goals

  • Binary search using the slope between neighbours instead of a target value
  • Recognise a monotone predicate hidden in a non-sorted list
Starting Python…