Welcome to the Mountain Hopping Challenge! ๐๏ธ
You are a skilled mountain climber standing on an array of peaks, where each element arr[i] represents the height of the i-th mountain. You have a special jumping ability that allows you to leap up to d positions in either direction.
The Rules:
- You can jump from index
ito any indexjwhere|i - j| โค d - You can only jump downward:
arr[i] > arr[j] - Your path must be clear: all peaks between your starting and landing positions must be shorter than your starting peak
Your goal is to find the maximum number of peaks you can visit by choosing any starting position and making optimal jumps. Each position can only be visited once during your journey.
Example: Given arr = [6,4,14,6,8,13,9,7,10,6,12] and d = 2, you might start at the tallest peak (height 14) and visit multiple shorter peaks, maximizing your journey length.
Input & Output
Visualization
Time & Space Complexity
For each starting position (n), we might explore up to d branches at each level, with maximum depth of n
Recursion stack can go up to n levels deep in the worst case
Constraints
- 1 โค arr.length โค 1000
- 1 โค arr[i] โค 105
- 1 โค d โค arr.length
- You can only jump to strictly lower positions
- All peaks between start and end must be lower than start