hardDynamic ProgrammingDynamic Programming

Frog Jump

hardTime: O(n²)Space: O(n²)

Recognize the pattern

frog jumping on stonesnext jump depends on last jumpstate = (position, last jump size)

Brute force idea

Try all jump sequences — exponential.

Better approach

Map each stone to set of reachable jump sizes. For each (stone, k): try k-1, k, k+1 to next stones. O(n²).

Key invariant

State = (position, last jump). Same position with different last jumps has different options. Set per stone avoids duplicates.

Watch out for

Not tracking jump size — options depend on how you arrived, not just where you are.

Dynamic Programming Pattern