Imagine you're a frog hopping across lily pads in a magical pond! πΈ You're given an integer array arr representing the heights of lily pads. Starting from any pad, you can make a series of jumps following special rules:
Odd-numbered jumps (1st, 3rd, 5th, ...): Jump to the closest pad that's at least as high as your current pad. If multiple pads have the same height, choose the leftmost one.
Even-numbered jumps (2nd, 4th, 6th, ...): Jump to the closest pad that's at most as high as your current pad. If multiple pads have the same height, choose the leftmost one.
Your goal is to reach the last lily pad (index arr.length - 1). A starting position is considered "good" if you can successfully reach the end by following these jumping rules.
Return the number of good starting positions.
Input & Output
Constraints
- 1 β€ arr.length β€ 2 Γ 104
- 0 β€ arr[i] < 105
- Array values can contain duplicates
- Must reach the last index (arr.length - 1) exactly