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 cannot jump directly to distant positions because intermediate heights violate the ascending jump rule (intermediates must be < starting height). Must go step by step: 0→1→2→3→4. Cost: 1 + 1 + 1 + 1 + 1 = 5, but we start at position 0 which costs 1, so path cost is 1 (start) + 1 (pos 1) + 1 (pos 2) + 1 (pos 3) + 1 (pos 4) = 5. Wait, let me recalculate properly: we can go 0→1→2→4. From 2 (height 3) to 4 (height 5), intermediate 3 (height 4) ≥ 3, so this is invalid for ascending. Actually optimal is 0→1→4 if valid. From 1 (height 2) to 4 (height 5), intermediates 2,3 have heights 3,4 which are ≥2, invalid. So must go 0→1→2→3→4 giving cost 5.
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 - Parkour Platform Navigation INPUT Platform Heights (nums) 3 i=0 2 i=1 4 i=2 1 i=3 2 i=4 START END Costs Array 3 4 2 3 1 Jump Rules: Ascending: nums[i] <= nums[j] Descending: nums[i] > nums[j] (intermediate constraints apply) ALGORITHM STEPS 1 Initialize DP dp[0]=0, dp[i]=INF 2 Find Valid Jumps Use monotonic stacks 3 Update DP States dp[j] = min(dp[j], dp[i]+cost[j]) 4 Return dp[n-1] Minimum cost to end DP State Table i=0 i=1 i=2 i=3 i=4 0 4 2 5 6 Optimal Path 0 +2 2 +4 4 FINAL RESULT Minimum Cost 6 OK - Verified Optimal Jump Path: 0 --> 2 --> 4 Platform 0 (cost: 0) Platform 2 (cost: +2) Platform 4 (cost: +4) Cost Breakdown: 0 + 2 + 4 = 6 Total Energy Used Key Insight: Use two monotonic stacks to efficiently find valid jump targets satisfying ascending/descending constraints. DP transition: dp[j] = min(dp[j], dp[i] + costs[j]) where i --> j is a valid jump. Time: O(n), Space: O(n). The greedy approach doesn't work because taking a higher cost jump early might enable cheaper jumps later. TutorialsPoint - Jump Game VIII | Dynamic Programming with Monotonic Stack
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