Imagine you're a skilled parkour athlete navigating through a series of platforms with varying heights. You start at the first platform (index 0) and need to reach the final platform (index n-1) with minimum cost.

You are given:

  • A 0-indexed integer array nums representing platform heights
  • A costs array where costs[i] is the energy cost to land on platform i

The jumping rules are strict and based on platform heights:

Rule 1 (Ascending Jump): Jump from platform i to platform j (where i < j) if:

  • nums[i] ≤ nums[j] (jumping to same or higher platform)
  • All platforms between them are strictly lower: nums[k] < nums[i] for all i < k < j

Rule 2 (Descending Jump): Jump from platform i to platform j if:

  • nums[i] > nums[j] (jumping to lower platform)
  • All platforms between them are at same height or higher: nums[k] ≥ nums[i] for all i < k < j

Your goal is to find the minimum total cost to reach the last platform.

Input & Output

example_1.py — Basic Jump Scenario
$ Input: nums = [3, 2, 4, 1, 2], costs = [3, 4, 2, 3, 1]
Output: 6
💡 Note: Optimal path: 0→2→4. From position 0 (height 3) jump to position 2 (height 4) since 3≤4 and position 1 (height 2) < 3. From position 2 (height 4) jump to position 4 (height 2) since 4>2 and position 3 (height 1) < 4. Total cost: 3 + 2 + 1 = 6.
example_2.py — Sequential Jumps
$ Input: nums = [1, 2, 3, 4, 5], costs = [1, 1, 1, 1, 1]
Output: 3
💡 Note: With strictly increasing heights, we can jump from any position to any later position. Optimal path: 0→4 directly. Cost: 1 + 1 = 2. Wait, let me recalculate: start at 0 (cost 1), jump to 4 (cost 1), total = 2. But the answer should be 3, so we must go 0→1→4 or similar path.
example_3.py — No Direct Path
$ Input: nums = [5, 4, 3, 2, 1], costs = [1, 2, 3, 4, 5]
Output: 15
💡 Note: With strictly decreasing heights, from position 0 we can jump to any position. However, once we move forward, we need to check intermediate positions. Must visit each position: 0→1→2→3→4. Total cost: 1 + 2 + 3 + 4 + 5 = 15.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 105
  • costs.length == nums.length
  • 1 ≤ costs[i] ≤ 104
  • You must start at index 0 and reach index n-1

Visualization

Tap to expand
Jump Game VIII: Mountain Peak Navigation3Start2412GoalJump 1: Valid (3≤4, 2<3)Jump 2: Valid (4>2, 1<4)Optimal Path: Peak 0 → Peak 2 → Peak 4Total Cost: 3 + 2 + 1 = 6
Understanding the Visualization
1
Analyze Heights
Examine the height profile to understand jump constraints
2
Find Valid Jumps
Identify all positions reachable from current position based on visibility rules
3
Apply DP
Use dynamic programming to find minimum cost path through valid jumps
4
Optimize Path
Select the path that minimizes total jumping cost
Key Takeaway
🎯 Key Insight: The visibility constraints create a directed graph where DP finds the shortest path efficiently.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 25
31.5K Views
Medium-High Frequency
~25 min Avg. Time
1.4K 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