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
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.
1FUNCTION canJump(nums):2 reach = 03 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 true7 RETURN true
← / → step · space play · Home restart