i has height heights[i]. A frog picks a starting post and only hops forward (to posts with a larger index). Its hops alternate between two kinds:
- **hops 1, 3, 5, …** go up: the frog lands on the lowest post ahead that is not lower than its current one;
- **hops 2, 4, 6, …** go down: the frog lands on the highest post ahead that is not higher than its current one.
If several posts of the same height qualify, the frog takes the nearest one. If no post qualifies, the run stops there.
A start is good if the frog can reach the last post from it (zero hops counts, so the last post is always good). Return the number of good starts.heights=[1, 3, 2] → 2
From post 0 the first (upward) hop goes to the lowest height that is at least 1 — that is 2, the last post. From post 1 there is no height of at least 3 ahead. The last post is good on its own. That makes 2.
heights=[5, 5, 5] → 3
With equal heights each hop lands on the nearest post, so the frog reaches the end from every start.
heights=[4, 2, 6, 3, 5] → 3
heights) ≤ 2·10⁴heights[i] < 10⁵heights=[1, 3, 2]
2
Run checks the first 3 tests in your browser. Submit checks all 202 tests on the server, hidden ones included.