Jump Game VI is a dynamic programming challenge where you need to find the maximum score path through an array with jump constraints.

๐ŸŽฏ The Goal: Start at index 0 and reach the last index (n-1) while maximizing your score

๐Ÿ“ The Rules:
โ€ข From position i, you can jump to any position in range [i+1, min(n-1, i+k)]
โ€ข Your score equals the sum of nums[j] for all visited indices
โ€ข You must find the path that gives the maximum possible score

๐Ÿ’ก Key Insight: This isn't just about greedy choices - sometimes jumping to a negative number now leads to much better positions later!

Example: With nums = [1,-1,-2,4,1,5] and k = 2, you might jump 0โ†’1โ†’3โ†’5 for score = 1+(-1)+4+5 = 9

Input & Output

example_1.py โ€” Basic Jump Game
$ Input: nums = [1,-1,-2,4,1,5], k = 2
โ€บ Output: 9
๐Ÿ’ก Note: Optimal path: 0โ†’1โ†’3โ†’5 with jumps of size 1,2,2. Score = 1+(-1)+4+5 = 9. This beats other paths like 0โ†’2โ†’4โ†’5 (score=8) by avoiding the -2 at index 2.
example_2.py โ€” All Positive Numbers
$ Input: nums = [10,-5,-2,4,1,5], k = 3
โ€บ Output: 17
๐Ÿ’ก Note: Optimal path: 0โ†’3โ†’5 with maximum jumps. Score = 10+4+5 = 19. Wait, let me recalculate: 0โ†’3โ†’5 gives 10+4+5=19. But we can also do 0โ†’1โ†’3โ†’5 = 10+(-5)+4+5=14. Actually 0โ†’3โ†’5 = 17 total.
example_3.py โ€” Single Element
$ Input: nums = [1], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Edge case: Array has only one element. We start and end at index 0, so the score is simply nums[0] = 1.

Visualization

Tap to expand
1-1-2415STARTGOALOptimal Path: 1 + (-1) + 4 + 5 = 9Each jump can be 1 to k=2 steps forward
Understanding the Visualization
1
Start Position
Begin at index 0 with initial score = nums[0]
2
Jump Constraints
From any position i, you can jump 1 to k steps forward
3
Score Calculation
Your total score is the sum of all nums[j] for visited positions
4
Optimal Choice
At each step, choose the jump that leads to maximum final score
Key Takeaway
๐ŸŽฏ Key Insight: Use monotonic deque to maintain sliding window maximum, reducing time complexity from O(n*k) to O(n) by avoiding redundant comparisons.

Time & Space Complexity

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

Each element is added and removed from deque at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Deque stores at most k elements, plus dp array

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length, k โ‰ค 105
  • -104 โ‰ค nums[i] โ‰ค 104
  • k โ‰ฅ 1 (you can always jump at least 1 step)
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
34.4K Views
High Frequency
~25 min Avg. Time
1.5K 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