teacher
Sign in

1D DP Patterns

The four one-dimensional DP shapes you meet most - take or skip, minimum coins, counting ways, and jumps - each with state, transition, base case and answer.

40 minCF 1200–17003 interactive9 problems

The problem

Most early DP problems have a state that is a single index or a single number: "the best answer using the first i items" or "the best answer for total s". That is a 1D DP, a table with one dimension.

This lesson covers four shapes. Each is the same loop with a different transition.

ShapeState dp[i]Transition idea
Take or skipbest value using the first i itemsskip item i, or take it and skip its neighbour
Min coinsfewest coins to make amount itry every coin as the last one
Count waysnumber of ways to make iadd the ways of every smaller state
Jumpsfewest jumps to reach position itry every position that can jump to i
1 of 7