Frog Jump
A medium Dynamic Programming problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
- Platform
- LeetCode
The problem
A frog is crossing a river. The river is divided into units, and at each unit there may or may not be a stone. The frog starts on the first stone and can jump k units. If its last jump was k units, its next jump can be k-1, k, or k+1 units. Determine if the frog can reach the last stone.
Example 1
- Input
- stones = [0,1,3,5,6,8,12,17]
- Output
- true
- Why
- The frog can jump 1 unit to stone 1, then 2 units to stone 3, then 2 units to stone 5, then 3 units to stone 8, then 4 units to stone 12, and 5 units to stone 17.
Example 2
- Input
- stones = [0,1,2,3,4,8,9,11]
- Output
- false
- Why
- There is no way to reach the last stone because the gap between stones 4 and 8 is too large.
Example 3
- Input
- stones = [0,1]
- Output
- true
- Why
- The frog jumps 1 unit to the last stone.
Constraints
- 2 <= stones.length <= 2000
- 0 <= stones[i] <= 2^31 - 1
- stones[0] == 0
- stones is sorted in ascending order
How to think about it
Updated 2026-09-09The state at any stone depends on how fast the frog was moving when it landed: stone position alone is not enough, but the incoming jump distance k is. Associate with each stone the set of jump sizes that can reach it, and propagate forward jumps of size k-1, k, and k+1 to stones matching position + step.
Approaches, worst first
Depth-first search with position and jump
time O(3^n) · space O(n^2)
From stone i with incoming jump k, branch into k-1, k, k+1. Without caching visited (index, k) pairs, routes crossing stones in different orders duplicate exponential paths.
DP over reachable jump setsWrite this one
time O(n^2) · space O(n^2)
Store a map mapping stone position to a set of possible incoming jump lengths. For each stone pos, iterate its incoming jumps k: for step in [k-1, k, k+1], if step > 0 and pos + step exists in the map, add step to the destination stone's jump set.
Where people lose marks · 3
- First jump must be exactly 1 unit to stone 1: if stones[1] != 1, the frog cannot even make its first move.
- Allowing jump sizes <= 0: if k - 1 == 0, attempting a jump of 0 keeps the frog stuck in place indefinitely.
- Using a dense 2D array indexed by stone positions. Stone coordinates can reach 2^31 - 1, which will exhaust memory unless coordinates are indexed by array indices or stored in hash structures.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
What each operation costs
| Operation | Time |
|---|---|
| fill dynamic programming table of n states | O(n) |
| solve two-dimensional grid of m by n states | O(m * n) |
| space-optimized state transition keeping one row | O(n) |
What usually goes wrong with Dynamic Programming
- Filling a bottom-up table in an order where the current cell needs values that have not been computed yet, reading uninitialized zeros.
- Failing to initialize base cases properly, such as filling a minimization table with zeros instead of infinity, which traps the answer at zero.
- Overwriting values in a 1D space-optimized knapsack array by scanning in the wrong direction, allowing the same item to be chosen multiple times.
Which roles need this problem
Dynamic Programming is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Game Developer, Cryptography Engineer, Performance Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Dynamic Programming problems
Problem set and role mapping as of .