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 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code