Skip to Content

How do you spot a DP problem quickly?

Category: DSA Patterns

Answer

Two keys: overlapping subproblems (same state computed many times without memo) and an optimal substructure (the answer for N uses answers for smaller subproblems). If a recursive solution overlaps, DP is little more than adding a cache.

Concrete examples from the fca project context

Example 1

Fibonacci with memo: def f(n): return f(n-1)+f(n-2); add @lru_cache and it’s O(N).

Example 2

Climbing stairs with K steps: f(n) = sum(f(n-k) for k in 1..K); cache the state.

Example 3

Longest common subsequence: f(i,j) uses f(i-1,j-1), f(i-1,j), f(i,j-1).

Example 4

House robber: f(i) = max(f(i-2) + house[i], f(i-1)).

Data flow / flow chart

recursive f(n) -> memoize -> tabulate -> O(N)
  (overlap + optimal substructure = DP)

Takeaway

DP “feels magical” until you see “overlapping subproblems + optimal substructure” twice in a row. Then it’s mechanical.