Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Dynamic Programming Patterns
DSA

Dynamic Programming Patterns

Learn how to identify overlapping subproblems, define states, and build efficient dynamic programming solutions.

Pattern / CategoryTypical Question TypesKeywords in QuestionWhy Use / Notes
1D DP (Linear) – CommonMax sum subarray, Min cost, Stepsmaximum, minimum, stepsRecurrence based on previous state; linear array DP; universal for arrays/DP
2D DP – CommonMatrix path sum, Grid problemsgrid, matrix, pathDP table based on neighbors; universal for matrices/grids
Knapsack / Subset Sum – CommonWeight/value optimizationcapacity, weight, max valueClassic optimization problem; track best value for given capacity
Unbounded Knapsack – Rare / BonusInfinite item selectionweight, value, repeatCan pick items multiple times; DP similar to knapsack but with repetition
Longest Increasing Subsequence (LIS) – CommonLIS, sequence problemsincreasing, subsequenceTrack subsequences using DP or patience sorting; common interview problem
DP on Trees – CommonMax path sum, Diameter, Rob houseschild, subtree, parentPost-order traversal DP; tree-specific patterns
DP with Memoization – CommonRecursive problemscache, recursion, overlappingAvoid recomputation; recursive approach with memoization
State Machine DP – Rare / BonusString parsing, Automatastate, transitionTrack multiple states over steps; used for strings, automata, or parsing DP
Probability / Digit DP – Rare / BonusProbability, countingprobability, digitsModel probabilistic events; count sequences or digit constraints
Bitmask DP – Rare / BonusTraveling salesman, Subset problemssubset, mask, stateEncode subset states as bits; ideal for TSP or combinatorial subset DP
Matrix Chain Multiplication (MCM) – Rare / BonusOptimal multiplication ordermatrices, costDP over intervals; find minimal multiplication cost for sequence of matrices

Dynamic Programming – Detection & Usage Guide

Rarity Legend:

  • Common / Very Common – Appears frequently in interviews; must know deeply.
  • Rare / Bonus – Appears occasionally or in advanced contests; important for specialized DP problems.

1. 1D DP (Linear) – Common

When to use / Detection cues:

  • Input: Linear array or sequence.
  • Keywords: maximum, minimum, steps.
  • Hints: Each state depends on previous state(s); optimize sum, cost, or steps.
  • Typical questions: Max sum subarray, Min cost path in array, Step/ladder problems. Mental trigger: “Linear sequence” + “previous step/state dependency”.

2. 2D DP – Common

When to use / Detection cues:

  • Input: Matrix or grid.
  • Keywords: grid, matrix, path.
  • Hints: Recurrence based on neighboring cells (up, left, diagonals).
  • Typical questions: Grid unique paths, Max path sum, Min path cost. Mental trigger: “Matrix/grid” + “neighbor dependency”.

3. Knapsack / Subset Sum – Common

When to use / Detection cues:

  • Input: Array of weights/values and capacity.
  • Keywords: capacity, weight, max value.
  • Hints: Optimize value under weight constraints.
  • Typical questions: 0/1 Knapsack, Subset sum problem, Max value with weight limit. Mental trigger: “Optimize sum/value under capacity constraint”.

4. Unbounded Knapsack – Rare / Bonus

When to use / Detection cues:

  • Input: Array of weights/values; items can repeat.
  • Keywords: repeat, infinite, weight, value.
  • Hints: Like knapsack but can take items multiple times.
  • Typical questions: Coin change (min coins), Max value with unlimited items. Mental trigger: “Can pick items multiple times”.

5. Longest Increasing Subsequence (LIS) – Common

When to use / Detection cues:

  • Input: Array or sequence.
  • Keywords: increasing, subsequence.
  • Hints: Find max-length increasing subsequence; patience sorting possible.
  • Typical questions: LIS length, Max sum increasing subsequence, Constrained subsequences. Mental trigger: “Sequence increasing / subsequence problem”.

6. DP on Trees – Common

When to use / Detection cues:

  • Input: Tree / hierarchical structure.
  • Keywords: child, parent, subtree.
  • Hints: Post-order traversal DP to solve for max/min path, sum, or other subtree properties.
  • Typical questions: Max path sum in tree, Rob houses problem, Diameter of tree. Mental trigger: “Tree input” + “compute value from children upwards”.

7. DP with Memoization – Common

When to use / Detection cues:

  • Input: Any structure with recursive overlapping subproblems.
  • Keywords: cache, recursion, overlapping.
  • Hints: Recursive solution with caching to avoid recomputation.
  • Typical questions: Fibonacci, Recursive grid paths, Recursive knapsack. Mental trigger: “Recursion with overlapping subproblems”.

8. State Machine DP – Rare / Bonus

When to use / Detection cues:

  • Input: String, automata, or stepwise transitions.
  • Keywords: state, transition.
  • Hints: Track multiple states over steps; DP table stores state at each step.
  • Typical questions: String parsing, Regular expression matching, Automata simulation. Mental trigger: “Stepwise transitions / state tracking”.

9. Probability / Digit DP – Rare / Bonus

When to use / Detection cues:

  • Input: Counting/probability problem, often with digits or sequences.
  • Keywords: probability, digits.
  • Hints: Model event probability or count sequences; DP table tracks state probabilities.
  • Typical questions: Count numbers with constraints, Probability of events, Digit DP problems. Mental trigger: “Count / probability” + “DP table to track states”.

10. Bitmask DP – Rare / Bonus

When to use / Detection cues:

  • Input: Small n (≤20), subsets or combinatorial states.
  • Keywords: mask, subset, state.
  • Hints: Encode subset states as bits; iterate 0..2^n-1.
  • Typical questions: Traveling salesman problem, Subset sum with constraints, Task assignment problems. Mental trigger: “Subset / mask / combinatorial DP”.

11. Matrix Chain Multiplication (MCM) – Rare / Bonus

When to use / Detection cues:

  • Input: Sequence of matrices.
  • Keywords: matrices, cost.
  • Hints: DP over intervals to find optimal multiplication order.
  • Typical questions: Optimal matrix multiplication order, Minimize scalar multiplications. Mental trigger: “Sequence of matrices” + “interval optimization”.

My Private Notes

Notes are auto-saved locally to this device.