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 i to any index j where |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
๐Ÿ”๏ธ Mountain Hopping Adventure641468START๐ŸŽฏ Strategy:1. Start from the tallest peak (height 14)2. Jump to reachable lower peaks within distance d=23. Optimal path: 14โ†’6โ†’4โ†’6 = 4 peaks visited!
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!
Asked in
Google 47 Amazon 32 Meta 28 Microsoft 23
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen