heights, the elevations of its points in order.
Call a contiguous stretch of the route a *ridge* if it has at least 3 points and the elevation first strictly rises and then strictly falls. The summit cannot be the first or the last point of the stretch: there must be both a climb and a descent.
Return the length of the longest ridge (the number of points in it), or 0 if the route has no ridge.
Try to do it in a single pass with O(1) extra memory.heights=[3, 1, 2, 6, 8, 4, 4, 2] → 5
The ridge 1, 2, 6, 8, 4 climbs to 8 and drops to 4. The next 4 is flat ground, which ends the descent. Length 5.
heights=[1, 2, 3, 4] → 0
The route only climbs and never descends, so there is no ridge and the answer is 0.
heights=[0, 5, 0, 3, 2, 1, 0] → 5
There are ridges 0, 5, 0 (length 3) and 0, 3, 2, 1, 0 (length 5). They share the 0 between them, which is fine.
heights=[3, 1, 2, 6, 8, 4, 4, 2]
5
Run checks the first 3 tests in your browser. Submit checks all 162 tests on the server, hidden ones included.