Visualize

Pattern visualizer

Jump Game

Instead of exploring every jump combination, the greedy sweep keeps ONE number: reach, the farthest index any sequence of jumps can touch so far. Scan left to right — if the current index is within reach, its jump may extend the frontier; if the index ever passes reach, no jump could have carried you there and the answer is false. Animated on: Jump Game: in [3, 1, 0, 2, 5, 1, 1] each value is the max jump length from that cell — can you reach the last index (6)?.

Greedy farthest-reach sweep

time O(n)space O(1)step 1 / 10
3
[0]
1
[1]
0
[2]
2
[3]
5
[4]
1
[5]
1
[6]
line 2

Each value is the FARTHEST you may hop forward from that cell. Greedy tracks a single frontier: reach = 0, the farthest index provably reachable. Goal: touch index 6.

Pseudocode
1FUNCTION canJump(nums):
2 reach = 0
3 FOR i from 0 to n-1:
4 IF i > reach: RETURN false (stranded)
5 reach = the larger of reach and i + nums[i]
6 IF reach >= n-1: RETURN true
7 RETURN true

← / → step · space play · Home restart

Where to practice Greedy