Top of the Mountain

EasyBinary Search≈ 20 min

A trail's heights are written in the list heights, one per marker. The trail climbs strictly to a single peak and then falls strictly to the end: every height before the peak is lower than the next one, and every height after it is lower than the one before.

Return the index of the peak. The peak is never the first or the last marker.

Examples

text
heights = [1, 4, 9, 7, 2]   →  2
heights = [0, 10, 5]        →  1
heights = [2, 3, 4, 5, 1]   →  3
heights = [1, 9, 8, 7, 6]   →  1

Constraints

  • Between 3 and 100,000 heights, each between 0 and 1,000,000,000, climbing to one peak and then falling.
  • Aim for O(log n): a trail four times longer should take only a couple of steps more.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.