Ctrl K

Frog on the Posts

Hard
A row of posts stands in a line; post 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.
Example 1
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.

Example 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.

Example 3
heights=[4, 2, 6, 3, 5] → 3
Constraints
  • 1 ≤ len(heights) ≤ 2·10⁴
  • 0 ≤ heights[i] < 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardRaising by Ranges HardBooks in Rising Order HardLong Leaps Across a Board Hard
loading Python…
Input
heights=[1, 3, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 202 tests on the server, hidden ones included.