Jump Game VI - Problem
You are given a 0-indexed integer array nums and an integer k.
You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive.
You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array.
Return the maximum score you can get.
Input & Output
Example 1 — Basic Jump Game
$
Input:
nums = [1,-1,-2,4,-7,3], k = 2
›
Output:
7
💡 Note:
You can jump from index 0 → 3 → 5. Score = 1 + 4 + 3 = 8. But optimal path is 0 → 1 → 3 → 5 with score 1 + (-1) + 4 + 3 = 7.
Example 2 — Large Jumps
$
Input:
nums = [10,-5,-2,4,0,3], k = 3
›
Output:
17
💡 Note:
Optimal path: 0 → 3 → 5. Score = 10 + 4 + 3 = 17. With k=3, we can jump up to 3 steps, so from index 0 we can reach indices 1, 2, or 3.
Example 3 — Single Element
$
Input:
nums = [1], k = 1
›
Output:
1
💡 Note:
Only one element in array, so the score is nums[0] = 1.
Constraints
- 1 ≤ nums.length ≤ 105
- -104 ≤ nums[i] ≤ 104
- 1 ≤ k ≤ nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with values and maximum jump distance k
2
Process
Find path from start to end maximizing sum of visited values
3
Output
Maximum possible score
Key Takeaway
🎯 Key Insight: Use monotonic deque to efficiently track the best position to jump from in the current window
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code