Jump Game VI
Keep a deque of indices whose dp values decrease from front to back. For each i: drop the front if it is out of reach (< i − k). The front is now the best index to jump from, so dp[i] = nums[i] + dp[front]. Then pop from the back every index with dp ≤ dp[i] and push i.
An index is still useful only if it is in reach (index ≥ i − k) and no newer index has a dp value ≥ it.
Problem
You are given an integer array nums and a number k. You start on index 0.
In one move you can jump forward by 1 to k steps, as long as you stay inside the array. So from index i you can land on any index from i + 1 to min(n − 1, i + k).
Your score is the sum of nums[j] over every index j you land on, including index 0 and the last index. You must end on the last index, n − 1.
Return the maximum score you can get.
An integer array nums of length n, and an integer k.
A single integer: the best possible score.
Land on 1 → -1 → 4 → 3. The score is 1 − 1 + 4 + 3 = 7. You must skip -2 and -7, and with k = 2 you can't skip two in a row.
Land on 10 → 4 → 3. Jumping 3 steps from 10 skips both negatives. Score 17.
One best path is 1 → -5 → 4 → 3 → -3, which gives 0.
- • 1 ≤ n, k ≤ 10^5
- • −10^4 ≤ nums[i] ≤ 10^4
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I already knew the best score to reach every earlier index, how would I get the best score for index i?
- 2.
From which indices can I land on index i?
- 3.
Is index i − k one of them, or not?
- 4.
When I move from i to i + 1, how does the set of indices I can come from change?
- 5.
Once a newer index has a score at least as good as an older one, will the older one ever be the best place to jump from?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | dp[i] = nums[i] + max of the last k dp values, found by a loop | O(N · K) | O(N) |
| Better | Keep (dp, index) in a max-heap; pop tops that are out of reach | O(N log N) | O(N) |
| Optimal | Deque of indices with decreasing dp; front = best index in reach | O(N) | O(K) |
Why does this belong to this pattern?
- “jump forward by 1 to k steps”dp[i] depends on a window of the last k states.
- “maximum score”We need the **max** over that window — a sliding window maximum.
- “sum of every index you land on”dp[i] = nums[i] + best previous dp. The window max is over dp values, which are only known as we go.
This is the anchor of the deque-DP sub-pattern. The deque is the same as in Sliding Window Maximum; the only new thing is that the values in the window are dp values you compute on the fly. The next question, Constrained Subsequence Sum, uses the same deque but lets you start fresh (max(0, …)) and end anywhere.
Don't make this mistake
A jump of exactly k is allowed, so i − k can still reach i. Dropping it loses the best path in cases like [1, -10, -10, 1], k = 3, where the answer 2 needs a jump of exactly 3.
The back pops compare dp[i], which doesn't exist yet. And i can't jump to itself. Compute dp[i] first, then insert i.
Pattern confusion with Jump Game I/II, where a greedy "farthest reach" works because you only care whether (or how fast) you get there. Here every landing adds to the score. In [1, 2, 5] with k = 2, jumping straight to the biggest value 5 gives 6, but landing on 2 first gives 8. Local choices don't add up to the best total — that needs dp.
We are looking for the best place to jump from, which is the best score so far (dp), not the best single value. Copying Sliding Window Maximum without changing nums to dp gives wrong answers.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.