Show problems as
Classic problem #941EasyArray

Valid Mountain Array

Real-world version: Is the Survey Line a Single Ridge
Tracked in this browser only
Read Valid Mountain Array on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A survey walks a straight line and records the ground elevation at each station, giving heights in walking order.

The line is a single ridge when both of the following hold:

  • it has at least three stations;
  • there is a station p, neither the first nor the last, such that the elevations strictly increase from the first station up to p, and strictly decrease from p on to the last station.

Strictly means no two neighbouring stations on either side may record the same elevation. Return true when the line is a single ridge and false otherwise.

Examples

Example 1

Input
heights = [0, 3, 1]
Output
true

Station 1 is neither the first nor the last, the elevation rises from 0 to 3 before it, and falls from 3 to 1 after it.

Example 2

Input
heights = [4, 7, 9, 11]
Output
false

The elevations keep rising to the last station, so there is no station strictly inside the line with a fall after it.

Example 3

Input
heights = [1, 2, 2, 1]
Output
false

Stations 1 and 2 both record 2. That pair is neither a strict rise nor a strict fall, so no station splits the line into the two required parts.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_single_ridge(heights: list[int]) -> bool:
Java
public boolean isSingleRidge(int[] heights)