heights.
Call a contiguous stretch heights[l..r] (with l ≤ r) good if its first and last points both have the greatest height in that stretch. In other words, heights[l] == heights[r] and no point between them is higher.
Return the number of good stretches. A stretch of a single point counts too.heights=[2, 5, 1, 5, 3] → 6
All 5 single-point stretches count, plus [5, 1, 5]: both ends are 5 and nothing in between is higher. 6 in total.
heights=[4, 4, 4] → 6
Every one of the 6 stretches counts, since all heights are equal.
heights=[3, 7, 3] → 3
The stretch [3, 7, 3] fails because the 7 inside is higher than its ends. Only the 3 single-point stretches remain.
heights=[2, 5, 1, 5, 3]
6
Run checks the first 3 tests in your browser. Submit checks all 113 tests on the server, hidden ones included.