Greedy Algorithms8 sections · 316 units
Open in Course

Jump Game II - Algorithm

BFS-like levels

Think of it as BFS. Level 00 is index 00. Level 11 is all indices reachable in 11 jump. And so on. Track:

  • currentEnd: the furthest index reachable with current number of jumps: furthest: the furthest index reachable with one more jump: jumps: number of jumps made When you reach currentEnd, you must jump. Update currentEnd = furthest and increment jumps. Time: O(n)O(n). Space: O(1)O(1). Without this level-based view, you would try all jump sequences in exponential time.