Skip to content
We areTOP 1%
DSA · Pro · Pattern 1 of 9

2-D Dynamic Programming

20 problems.
Your progress0%
1st Visit
0/20
2nd Visit
0/20
Revise
0/20

About this pattern

Two-dimensional DP stores the answer to a subproblem indexed by two parameters: most often a pair of prefixes of two strings, a cell of a grid, or a position combined with a count or a small state. Recognise it when a statement compares two sequences, asks for the cheapest path or the number of ways across a grid, or limits how many items or segments may be chosen. Define the state precisely, write the transition from smaller indices, and fill the table in an order that respects those dependencies. Typical cost is O(n * m) time; when each row depends only on the previous one, memory drops to O(m).

// dp[i][j] describes the prefixes a[0..i) and b[0..j)
int twoSequenceDp(const string& a, const string& b) {
    int n = a.size(), m = b.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));   // base row and column
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);     // drop one element
            if (a[i - 1] == b[j - 1])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);  // use both
        }
    return dp[n][m];
}

Problems

2-D Dynamic Programming questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Unique Paths IILeetCodeMedium Solve Unique Paths II on LeetCode (opens in a new tab)
2Minimum Path SumLeetCodeMedium Solve Minimum Path Sum on LeetCode (opens in a new tab)
3TriangleLeetCodeMedium Solve Triangle on LeetCode (opens in a new tab)
4Basketball ExerciseCodeforcesMedium 1400Solve Basketball Exercise on Codeforces (opens in a new tab)
5Longest Common SubsequenceLeetCodeMedium Solve Longest Common Subsequence on LeetCode (opens in a new tab)
6Maximal SquareLeetCodeMedium Solve Maximal Square on LeetCode (opens in a new tab)
7Three StringsCodeforcesMedium 1500Solve Three Strings on Codeforces (opens in a new tab)
8Coin Change IILeetCodeMedium Solve Coin Change II on LeetCode (opens in a new tab)
9Longest Palindromic SubsequenceLeetCodeMedium Solve Longest Palindromic Subsequence on LeetCode (opens in a new tab)
10Edit DistanceLeetCodeMedium Solve Edit Distance on LeetCode (opens in a new tab)
11k-TreeCodeforcesMedium 1600Solve k-Tree on Codeforces (opens in a new tab)
12Hard problemCodeforcesMedium 1600Solve Hard problem on Codeforces (opens in a new tab)
13Make Them EqualCodeforcesMedium 1600Solve Make Them Equal on Codeforces (opens in a new tab)
14Zero PathCodeforcesMedium 1700Solve Zero Path on Codeforces (opens in a new tab)
15Caesar's LegionsCodeforcesMedium 1700Solve Caesar's Legions on Codeforces (opens in a new tab)
16Sleeping ScheduleCodeforcesMedium 1700Solve Sleeping Schedule on Codeforces (opens in a new tab)
17George and JobCodeforcesMedium 1700Solve George and Job on Codeforces (opens in a new tab)
18Distinct SubsequencesLeetCodeHard Solve Distinct Subsequences on LeetCode (opens in a new tab)
19Wildcard MatchingLeetCodeHard Solve Wildcard Matching on LeetCode (opens in a new tab)
20Cherry Pickup IILeetCodeHard Solve Cherry Pickup II on LeetCode (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account