2Brute2Best
πŸ”₯0

Jump Game II

MediumΒ·LeetCode #45 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You start on index 0 of an array nums. Each number is the longest jump you can make from that index: from index i you may land on any index from i + 1 up to i + nums[i], as long as you stay inside the array.

The last index can always be reached. Return the fewest jumps that get you from index 0 to the last index.

Input

An array of integers nums. nums[i] is the furthest forward jump from index i.

Output

The fewest number of jumps needed to reach the last index.

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

Jump 1 step from index 0 to index 1, then 3 steps from index 1 to the last index.

Example 2
Input: nums = [2, 3, 0, 1, 4]
Output: 2

Jump from index 0 to index 1, then from index 1 to index 4. The zero at index 2 is never needed.

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^4
  • β€’ 0 ≀ nums[i] ≀ 1000
  • β€’ The last index can always be reached.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œminimum number of jumps”
β†’ Each jump is one more round. The answer is how many rounds it takes to reach the end.
β€œmaximum jump length”
β†’ Every index in a round can start the next round, so the rounds are blocks that touch each other.
β€œyou can always reach the last index”
β†’ The rounds keep growing until they cover the last index, so the loop always finishes.
03

Approach Complexity Ladder

Brute Force β€” try every jump, keep the fewestO(2^N)O(N)

Recursively try every landing spot and keep the smallest jump count

Better β€” minJumps[i] table over earlier indexesO(NΒ²)O(N)

minJumps[i] is the fewest jumps to reach i, taken from the best earlier index that jumps to it

Optimal β€” count the rounds with curEnd and farO(N)O(1)

Scan once; add a jump each time the scan reaches the end of the current round

Brute Force Intuition

Let fewestFrom(i) be the fewest jumps from index i to the last index. If i is the last index, the answer is 0. Otherwise try each landing spot i + len for len from 1 up to nums[i], take 1 + fewestFrom(i + len), and keep the smallest.

Recognition rule for this sub-pattern:

Track the farthest point you can reach so far; the answer depends only on that frontier.
Remember this Β· Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

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 Greedy translate into production architectures and low-level memory mechanics.