Jump Game V - Problem
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
basic_case.py โ Python
$
Input:
arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
โบ
Output:
4
๐ก Note:
Starting from index 2 (height=14), we can visit: 14โ6โ4โ6, giving us 4 total positions visited. This is optimal since we start from the highest peak and jump to progressively lower peaks within the jump distance.
small_array.py โ Python
$
Input:
arr = [3,3,3,3,3], d = 3
โบ
Output:
1
๐ก Note:
All elements have the same height (3), so we can't make any jumps since we can only jump to strictly lower peaks. We can only visit 1 position (wherever we start).
limited_distance.py โ Python
$
Input:
arr = [7,6,5,4,3,2,1], d = 1
โบ
Output:
7
๐ก Note:
Perfect descending sequence with d=1. Starting from index 0 (height=7), we can jump to each adjacent lower element: 7โ6โ5โ4โ3โ2โ1, visiting all 7 positions.
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
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Landscape
Look at all the peaks and identify the tallest ones - these are your best starting points
2
Plan Your Route
From each tall peak, identify which shorter peaks you can reach within your jump distance
3
Execute Optimal Path
Start from the tallest accessible peak and follow the path that visits the most peaks
Key Takeaway
๐ฏ Key Insight: Process peaks from tallest to shortest using topological sorting. When we compute the optimal path from any peak, all reachable destinations have already been solved!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code