grid_viewDynamic Programming

Dynamic Programming: when to spot it, explain it, and practice it

Dynamic programming becomes manageable when you stop memorizing formulas and start asking what state must be remembered so the rest of the problem looks the same.

Pattern coverage

200+

Best first move

State must have a clear meaning you can say in one sentence.

Common failure point

State meaning is fuzzy, so the recurrence becomes random.

When this pattern should come to mind

You are optimizing, counting, or checking feasibility over many overlapping subproblems.
Greedy choices feel tempting but are hard to prove.
The answer for a position depends on a smaller prefix, suffix, or state.

Checklist before you code

State must have a clear meaning you can say in one sentence.
Base cases should be written before the recurrence.
Transitions must only depend on already-computed states.
Consider whether you can compress space after correctness is clear.

The solving flow that works well in interviews

1

Name the state in plain language.

2

List the decisions that can transition into the state.

3

Write base cases that match your state definition exactly.

4

Choose an iteration order that guarantees dependencies are ready.

5

Only then optimize space or constants.

Common variants

Linear DP

The next answer depends on a prefix or previous few positions.

Sequence DP

Two sequences or matching relationships define the state.

Decision DP

You choose whether to take, skip, merge, or split at each step.

Template preview

PythonPublic preview
# 1D DP
dp = [0] * (n + 1)
dp[0] = base
for i in range(1, n + 1):
    dp[i] = transition(dp, i)

# 2D DP
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, m + 1):
        dp[i][j] = transition(dp, i, j)

A more useful problem ladder for practice

This is not a random list. It is ordered to help candidates build recognition first, add key variants next, and then increase pressure with harder cases.

High-frequency mistakes

warning

State meaning is fuzzy, so the recurrence becomes random.

warning

Base cases do not match the state definition.

warning

Transition misses one legal choice or double-counts two paths.

warning

Space optimization is attempted before correctness is proven.

Recommended practice path

1

Start with linear DP like climbing stairs and house robber.

2

Move to sequence DP such as longest common subsequence.

3

Then add decision-heavy problems like coin change and decode ways.

4

Finish with interval or advanced state DP only after the basics feel natural.

Dynamic Programming Pattern Guide | LeetCode Interview Prep - Interview AiBox