Jump Game VIII - Problem
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
numsrepresenting platform heights - A costs array where
costs[i]is the energy cost to land on platformi
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 alli < 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 alli < 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
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
โ Quadratic Growth
Space Complexity
O(n)
DP array to store minimum costs for each position
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code