Data Structures and Algorithms • C# examples
Dynamic Programming: Overlapping Subproblems and State
Reuse subproblem results when a recursive problem repeatedly asks the same questions.
Concept
Dynamic programming (DP) applies when a problem has overlapping subproblems and an optimal substructure. A state must contain enough information to determine future choices; transitions describe how smaller states produce larger ones. The answer depends on a base case and evaluation order, not on memorizing a template.
How to approach it
Define state in words, write a recurrence, set base cases, determine dependencies, and then choose memoization or tabulation.
Complexity
Time and space equal the number of computed states times work per transition.
C# example
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i <= n; i++)
dp[i] = dp[i - 1] + (i >= 2 ? dp[i - 2] : 0);
Expected result or behavior
The state combines the number of ways to reach the previous one or two steps.
Practice exercise
Write the state meaning and recurrence for minimum coins needed to form a target amount.
Key takeaway: A correct DP starts with a precise state definition.