Odd Even Jump

A fleet of suspension trams runs along a straight mountain ridge. Each hub along the ridge is marked with an elevation, stored in the array heights. A tram dispatch always alternates between two routing rules, starting with rule A on the first hop.

  • Rule A hop: from hub i, the tram must land on the closest hub to its right whose elevation is at least the current hub's elevation. Among all eligible hubs, it picks the one with the smallest elevation, and if several share that elevation, the leftmost among them.
  • Rule B hop: from hub i, the tram must land on the closest hub to its right whose elevation is at most the current hub's elevation. Among all eligible hubs, it picks the one with the largest elevation, and if several share that elevation, the leftmost among them.

A good start is a hub where a tram can begin, take a Rule A hop, then alternate B, A, B, and so on, using the elevation ties as described, and eventually land on the final hub (the last index). A tram that already sits on the final hub counts as having made the journey. Reaching the end with an even total number of hops is allowed.

Return the number of good starts across the whole ridge.

Examples
Input: [10,13,12,14,15]
Output: 2
Hints

Odd Even Jump

A fleet of suspension trams runs along a straight mountain ridge. Each hub along the ridge is marked with an elevation, stored in the array `heights`. A tram dispatch always alternates between two routing rules, starting with rule **A** on the first hop.