20 Dynamic Programming Patterns to Master LeetCode: From Fibonacci to State Machines
This comprehensive guide breaks down 20 essential dynamic programming patterns — including Fibonacci, Kadane's algorithm, knapsack variants, LCS, LIS, edit distance, grid/tree/graph DP, digit DP, bitmask DP, probability DP, and state machines — with LeetCode practice problems for each pattern to build recognition and problem-solving fluency.
Reading font

Fibonacci Pattern
Applies when a problem's solution depends on solutions to smaller subproblems, following a clear recursive relation like F(n) = F(n-1) + F(n-2). Each step derives from the previous two steps.
LeetCode Problems
LeetCode 70: Climbing Stairs
LeetCode 509: Fibonacci Number
LeetCode 746: Min Cost Climbing Stairs
Ideal entry-level practice for understanding basic state transition.
Kadane's Algorithm
Solves maximum subarray sum and variants. Core idea: in one pass, maintain the optimal value of subarrays ending at each position to derive the global optimum — a classic DP application.
LeetCode Problems
LeetCode 53: Maximum Subarray
LeetCode 918: Maximum Sum Circular Subarray
LeetCode 152: Maximum Product Subarray
0/1 Knapsack Pattern
Use when: given items with weight and value, select a subset under a total weight constraint to maximize total value, each item used at most once (0 or 1).
LeetCode Problems
LeetCode 416: Partition Equal Subset Sum
LeetCode 494: Target Sum
LeetCode 1049: Last Stone Weight II
Fundamental model for subset selection with constraints.
Unbounded Knapsack Pattern
Same as 0/1 knapsack but each item can be chosen unlimited times. Common in coin change and combination counting.
LeetCode Problems
LeetCode 322: Coin Change
LeetCode 518: Coin Change II
LeetCode 279: Perfect Squares
Longest Common Subsequence (LCS)
Find the longest subsequence present in both sequences (order preserved, not necessarily contiguous). Key for string matching, diff tools, file comparison.
LeetCode Problems
LeetCode 1143: Longest Common Subsequence
LeetCode 583: Delete Operation for Two Strings
LeetCode 1092: Shortest Common Supersequence
Longest Increasing Subsequence (LIS)
Find the longest strictly increasing subsequence (not necessarily contiguous). Applications in sorting, nesting, optimal substructure. Note: besides classic DP, an advanced binary search optimization achieves O(n log n).
LeetCode Problems
LeetCode 300: Longest Increasing Subsequence
LeetCode 673: Number of Longest Increasing Subsequence
LeetCode 354: Russian Doll Envelopes
Palindromic Subsequence Pattern
Find longest palindromic subsequence/substring, or minimum insertions/deletions to make a string palindrome. Uses DP to exploit palindromic optimal substructure.
LeetCode Problems
LeetCode 516: Longest Palindromic Subsequence
LeetCode 647: Palindromic Substrings
LeetCode 1312: Minimum Insertion Steps to Make a String Palindrome
Edit Distance Pattern
Transform one string into another with minimum operations: insert, delete, substitute.
LeetCode Problems
LeetCode 72: Edit Distance
LeetCode 583: Delete Operation for Two Strings
LeetCode 712: Minimum ASCII Delete Sum for Two Strings
Subset Sum Pattern
Determine if a subset sums to a target. Essence: decide for each element to include or exclude — a 0/1 knapsack variant.
LeetCode Problems
LeetCode 416: Partition Equal Subset Sum
LeetCode 494: Target Sum
LeetCode 698: Partition to K Equal Sum Subsets
String Partition Pattern
Split a string into substrings meeting conditions, optimizing cost/count/ways. Characteristics: string/sequence problem, split by rules, find optimal split among all possibilities, subproblems combine, enumerate split points. Often uses interval DP dp[i][j].
LeetCode Problems
LeetCode 132: Palindrome Partitioning II
LeetCode 131: Palindrome Partitioning
LeetCode 139: Word Break
Catalan Numbers Pattern
Counts structures from recursive decomposition: valid parentheses combinations, distinct BSTs with n nodes, triangulations of an (n+2)-gon.
LeetCode Problems
LeetCode 96: Unique Binary Search Trees
LeetCode 22: Generate Parentheses
Matrix Chain Multiplication
Optimize operation order for a sequence of combinable elements where combination cost depends on order. Interval DP: enumerate all partitions, solve subintervals recursively.
LeetCode Problems
LeetCode 1039: Minimum Score Triangulation of Polygon
LeetCode 312: Burst Balloons
LeetCode 1000: Minimum Cost to Merge Stones
Counting Problems (Count Distinct Ways)
Count ways to reach a target. Conditions: count all valid paths/combinations, overlapping subproblems, combinatorial DP. Typically 1D DP where dp[i] = ways for first i elements. Key: correct state transition and boundary handling.
LeetCode Problems
LeetCode 91: Decode Ways
LeetCode 2266: Count Number of Texts
Grid DP (DP on Grids)
Find paths/optimal values in 2D grid. Traits: 2D array with state/cost per cell, moves right/down/diagonal, current cell depends on neighbors. Usually 2D DP array, space-optimizable to 1D or in-place.
LeetCode Problems
LeetCode 62: Unique Paths
LeetCode 64: Minimum Path Sum
LeetCode 329: Longest Increasing Path in a Matrix
Tree DP (DP on Trees)
For tree-structured data: decompose into subtree subproblems, merge bottom-up (post-order). Decisions at a node affect children/parent. Common: select nodes to maximize profit under constraints.
LeetCode Problems
LeetCode 337: House Robber III
LeetCode 124: Binary Tree Maximum Path Sum
LeetCode 968: Binary Tree Cameras
Graph DP (DP on Graphs)
Combine graph algorithms (BFS, Dijkstra, topological sort) with DP. Find optimal paths (shortest, longest, constrained, min-cost) where node state depends on neighbors. Track steps/cost during traversal to satisfy constraints.
LeetCode Problems
LeetCode 787: Cheapest Flights Within K Stops
LeetCode 1334: Find the City With the Smallest Number of Neighbors at a Threshold Distance
Digit DP
Count/sum numbers in a large range (up to 10^18) where each digit matters. Avoid brute force by DP on digit positions with constraints.
LeetCode Problems
LeetCode 357: Count Numbers with Unique Digits
LeetCode 233: Number of Digit One
LeetCode 902: Numbers At Most N Given Digit Set
Bitmask DP
Represent subset/combination states as bits of an integer. Useful when: elements ≤ 20–30, need efficient set operations, decisions are include/exclude or visit/unvisit, space optimization via bitwise state.
LeetCode Problems
LeetCode 1986: Minimum Number of Work Sessions to Finish the Tasks
LeetCode 2305: Fair Distribution of Cookies
LeetCode 847: Shortest Path Visiting All Nodes
Probability DP
Compute probabilities or expected values in random processes (random walks, dice, cards). State probability depends on previous states. Design dp[i][j] for probability/expectation at state (i,j). Transition formula replaces exhaustive enumeration.
LeetCode Problems
LeetCode 688: Knight Probability in Chessboard
LeetCode 808: Soup Servings
LeetCode 837: New 21 Game
State Machine DP
Model as finite states with transitions. Multi-stage decision optimization. Conditions: clear states and transitions, each operation moves between states, optimal solution depends on valid transition sequences, choices affect future states/rewards, finite enumerable states (e.g., hold stock, cooldown).
LeetCode Problems
LeetCode 309: Best Time to Buy and Sell Stock with Cooldown
LeetCode 123: Best Time to Buy and Sell Stock III
For questions about attribution, corrections or content rights, please contactand we will review it promptly.