Skip to content

Longest Mountain in Array

MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

A mountain is a contiguous stretch of a list with length at least 3 that strictly increases to a peak and then strictly decreases. The peak cannot be the first or last element of the stretch. Given a list arr, return the length of the longest mountain, or 0 if there is none.

arr = [2, 1, 4, 7, 3, 2, 5]   →  5   ([1, 4, 7, 3, 2])
arr = [2, 2, 2]               →  0   (flat is not a mountain)
arr = [1, 2, 3]               →  0   (no way down)

Can you do it in one pass with O(1) extra space?

Constraints

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 10^4