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
Jump Game VI: Maximum Score Path with k=21-1-24-73012345Optimal Path: 0 → 1 → 3 → 5 (jump at most k=2 steps)Score: 1 + (-1) + 4 + 3 = 7Maximum Score: 7
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
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
67.0K Views
Medium Frequency
~25 min Avg. Time
1.9K 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