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

  • Adjacent heights are never equal, and the list is a valid mountain.
  • Aim for O(log n) time: look at about 20 heights, not all of them. (A full scan still passes the tests; the point is to find the summit without it.)

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…