FunctionRacerProblemsRace

Frog on the Posts

HardStacks and queuesPython, JavaScript, TypeScript, Java, C++

Posts stand in a row and heights[i] is the height of post i. A frog only hops to the right, and its hops alternate:

When several posts are equally good, the frog takes the nearest. When there is no such post, it is stuck. Return how many posts are good starts: posts from which the frog reaches the last post. The last post itself is a good start.

Example 1

heights [5,1,3,4,2]
returns 3

Post 4 is the last post. From post 1 the up hop lands on the lowest higher post, which is the last one. From post 2 the frog hops up to post 3 and then down to post 4. Posts 0 and 3 have nothing high enough to their right.

Example 2

heights [6,6,6]
returns 3

Each post has a post of equal height right after it, so every hop works.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems