Greedy Algorithms8 sections · 316 units
Open in Course

Jump Game - Walkthrough

Tracing the algorithm

Example: [2,3,1,1,4]. 1.1. i= 00: furthest = max(00, 00+22) = 22.

2.2. i= 11: 11 <= 22, reachable. furthest = max(22, 11+33) = 44.

3.3. furthest= 44 >= n1n-1= 44. Done! Return true.

Example: [3,2,1,0,4]. 1.1. i= 00: furthest = 33.

2.2. i= 11: furthest = max(3, 3) = 33.

3.3. i= 22: furthest = max(3, 3) = 33.

4.4. i= 33: furthest = max(3, 3) = 33.

5.5. i= 44: 44 > 33. Cannot reach. Return false. Notice we detect failure the moment we try to visit an unreachable index.