2Brute2Best
🔥0

Jump Game VI

Medium·LeetCode #1696Basic Anchor
LeetCode
30-Second Mental ModelDP + Monotonic Decreasing Deque (sliding window max over dp)
Complexity:O(N · K)→O(N)
Key Insight

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.

Core Validity Invariant

An index is still useful only if it is in reach (index ≥ i − k) and no newer index has a dp value ≥ it.

01

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.

Input

An integer array nums of length n, and an integer k.

Output

A single integer: the best possible score.

Example 1
Input: nums = [1, -1, -2, 4, -7, 3], k = 2
Output: 7

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.

Example 2
Input: nums = [10, -5, -2, 4, 0, 3], k = 3
Output: 17

Land on 10 → 4 → 3. Jumping 3 steps from 10 skips both negatives. Score 17.

Example 3
Input: nums = [1, -5, -20, 4, -1, 3, -6, -3], k = 2
Output: 0

One best path is 1 → -5 → 4 → 3 → -3, which gives 0.

Constraints
  • • 1 ≤ n, k ≤ 10^5
  • • −10^4 ≤ nums[i] ≤ 10^4
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    If I already knew the best score to reach every earlier index, how would I get the best score for index i?

  2. 2.

    From which indices can I land on index i?

  3. 3.

    Is index i − k one of them, or not?

  4. 4.

    When I move from i to i + 1, how does the set of indices I can come from change?

  5. 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?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute Forcedp[i] = nums[i] + max of the last k dp values, found by a loopO(N · K)O(N)
BetterKeep (dp, index) in a max-heap; pop tops that are out of reachO(N log N)O(N)
OptimalDeque of indices with decreasing dp; front = best index in reachO(N)O(K)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “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.
dp[i] = value + best of the last k dp values+window slides one step each time+need the window max→DP + Monotonic Decreasing Deque (sliding window max over dp)

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.

10

Don't make this mistake

Off-by-one window: dropping index i − k
✗ if (dq.front() <= i - k) dq.pop_front();
✓ if (dq.front() < i - k) dq.pop_front();

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.

Pushing i before computing dp[i]
✗ while (...) dq.pop_back(); dq.push_back(i); dp[i] = nums[i] + dp[dq.front()];
✓ dp[i] = nums[i] + dp[dq.front()]; while (...) dq.pop_back(); dq.push_back(i);

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.

Greedy: always jump to the biggest value in reach

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.

Comparing nums instead of dp in the deque
✗ while (!dq.empty() && nums[dq.back()] <= nums[i]) dq.pop_back();
✓ while (!dq.empty() && dp[dq.back()] <= dp[i]) dq.pop_back();

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.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.