LeetCode 45 Jump Game II - Example and Complexity Analysis

Walkthrough and analysis

Trace nums = [2,3,1,1,4].

currentEnd = 0, farthest = 0, jumps = 0.

i=0: farthest = max(0, 0+2) = 2. i==currentEnd. Jump! jumps=1, currentEnd=2. i=1: farthest = max(2, 1+3) = 4. Not at currentEnd yet. i=2: farthest = max(4, 2+1) = 4. i==currentEnd. Jump! jumps=2, currentEnd=4.

Reached end. Answer: 22 jumps.

O(n)O(n) time, single pass. O(1)O(1) space.