↻one more pass.
Greedy · Medium

Jump Game II: the idea to remember.

Find the minimum number of forward jumps to reach the last array index; reachability is guaranteed.

Solve on LeetCode ↗

The core insight

Treat indices reachable in the same number of jumps as a layer and expand to the farthest next boundary.

Why it works

All positions in the current range cost the same jumps, so only their farthest possible extension matters for the next layer.

The mistake to avoid

Do not count a jump after already reaching the last index. A one element array needs zero jumps.

Time: O(n)Space: O(1)

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

// Java reference adapted from neetcode-gh/leetcode (MIT).
// Treat indices reachable in the same number of jumps as a layer and expand to the farthest next boundary.
public int jump(int[] nums) {
    int res = 0, r = 0, l = 0, fur = 0;

    while (r < nums.length - 1) {
        fur = 0;
        for (int i = l; i <= r; i++) fur = Math.max(fur, i + nums[i]);
        l = r + 1;
        r = fur;
        res++;
    }
    return res;
}

Java reference adapted from neetcode-gh/leetcode · MIT license

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.