DSA AnimatorREVISION VIDEOS
← Back to problems
New

Jump Game — LeetCode 55 video explanation

Each number is the maximum jump length from that position. Starting at index 0, decide whether you can reach the last index.

Loading videos…

DSA Animator › DSA Revision › Greedy › Jump Game

Key moments in the Jump Game video

0:03Problem0:11Example0:20Brute force0:30Key idea0:44Dry run1:01Code1:11Takeaway

What you will learn

Remember: track only the farthest reachable index. If you ever stand beyond it, you are stuck. The video solves Jump Game (LeetCode 55, Medium) with the Greedy Reach pattern from the Greedy topic, and you can replay every step in the interactive Jump Game animation.

Jump Game video transcript

Problem

Each number is the maximum jump length from that position. Starting at index 0, decide whether you can reach the last index.

Example

For 2, 3, 1, 1, 4, the answer is true. For 3, 2, 1, 0, 4, every path gets stuck at the 0, so the answer is false.

Brute force

Trying every possible jump from every index explodes, and even dynamic programming is O of n squared. We only need to know the farthest index we can reach.

Key idea

Keep a variable, far, for the farthest reachable index so far. At each index, if i is beyond far, we are stuck, so return false. Otherwise update far with i plus the jump length, and return true once far reaches the end.

Dry run

At index 0, far becomes 0 plus 2, which is 2. At index 1, far becomes 1 plus 3, which is 4. That already reaches the last index, so the answer is true. In the second array, far stays at 3, and index 4 is beyond it, so the answer is false.

Code

In code, loop over every index. If i is greater than far, return false. Update far, and return true after the loop, so it's O of n time and O of 1 space.

Takeaway

Remember: track only the farthest reachable index. If you ever stand beyond it, you are stuck.

More Greedy revision videos

← All 185 DSA revision videos