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.

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

Triple nested loop: O(n) positions ร— O(n) destinations ร— O(n) validation

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

DP array to store minimum costs for each position

n
2n
โšก Linearithmic Space

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
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