This is the sheet I recommend for learning dynamic programming (DP). It starts with the classic patterns, moves on to advanced techniques, and ends with practice problems to test yourself.
If you complete this sheet, you will be able to solve even tough DP problems on Codeforces, and you will never fear a DP problem again.
Solve it in order. Within each group, the problems build on each other, so try each one on your own before looking at a solution.
1.Conceptual problems
These cover the core DP patterns. Most DP problems you will meet are a variation of one of them.
Knapsack
- 0-1 Knapsack Problem (opens in a new tab)
- Target Sum (opens in a new tab)
- Coin Change (opens in a new tab)
- Partition Equal Subset Sum (opens in a new tab)
- Reducing Dishes (opens in a new tab)
- Rod Cutting (opens in a new tab)
Grid paths
- Unique Paths (opens in a new tab)
- Unique Paths II (opens in a new tab)
- Dungeon Game (opens in a new tab)
Longest increasing subsequence
- Longest Increasing Subsequence (opens in a new tab)
- Box Stacking (opens in a new tab)
- Russian Doll Envelopes (opens in a new tab)
Longest common subsequence
- Longest Common Subsequence (opens in a new tab)
- Longest Common Substring (opens in a new tab)
- Shortest Common Supersequence (opens in a new tab)
- Minimum number of deletions and insertions (opens in a new tab)
- LCS of three strings (opens in a new tab)
Counting
- Consecutive 1's not allowed (opens in a new tab)
- Count possible ways to construct buildings (opens in a new tab)
- Count subsequences of type a^i, b^j, c^k (opens in a new tab)
- Number of Ways to Paint N × 3 Grid (opens in a new tab)
Buy and sell stock
- Best Time to Buy and Sell Stock (opens in a new tab)
- Best Time to Buy and Sell Stock II (opens in a new tab)
- Best Time to Buy and Sell Stock with Transaction Fee (opens in a new tab)
- Best Time to Buy and Sell Stock with Cooldown (opens in a new tab)
- Best Time to Buy and Sell Stock III (opens in a new tab)
- Best Time to Buy and Sell Stock IV (opens in a new tab)
Matrix chain multiplication
- Matrix Chain Multiplication (opens in a new tab)
- Palindrome Partitioning (opens in a new tab)
- Scramble String (opens in a new tab)
- Boolean Parenthesization (opens in a new tab)
2.CSES problem set
After the conceptual problems, solve every DP problem in the CSES problem set (opens in a new tab). You can check your approach against my CSES DP solutions (opens in a new tab).
3.Bitmask DP
- Minimum Number of Work Sessions to Finish the Tasks (opens in a new tab)
- Partition to K Equal Sum Subsets (opens in a new tab)
- U - Grouping (opens in a new tab)
- COURIER - The Courier (opens in a new tab)
- BABY - Baby (opens in a new tab)
- HELPBOB - Help Bob (opens in a new tab)
4.Digit DP
- PR003004 - Digit Sum (opens in a new tab)
- Count of Integers (opens in a new tab)
- Unlucky Numbers (opens in a new tab)
5.Meet-in-the-middle
- Closest Subsequence Sum (opens in a new tab)
- Partition Array Into Two Arrays to Minimize Sum Difference (opens in a new tab)
6.Practice problems
Once you know the patterns, use these to test yourself. Nobody tells you which pattern a problem needs, so spotting it is part of the practice.
LeetCode medium
- Ugly Number II (opens in a new tab)
- Super Ugly Number (opens in a new tab)
- Minimum Score Triangulation of Polygon (opens in a new tab)
- Fair Distribution of Cookies (opens in a new tab)
- Maximum Number of Points with Cost (opens in a new tab)
LeetCode hard
- Wildcard Matching (opens in a new tab)
- Regular Expression Matching (opens in a new tab)
- Maximum Profit in Job Scheduling (opens in a new tab)
- Edit Distance (opens in a new tab)
- Burst Balloons (opens in a new tab)
- Tallest Billboard (opens in a new tab)
- Student Attendance Record II (opens in a new tab)
- Number of Increasing Paths in a Grid (opens in a new tab)
- Paths in Matrix Whose Sum Is Divisible by K (opens in a new tab)
- Super Egg Drop (opens in a new tab)
- Number of Music Playlists (opens in a new tab)
- Minimum Cost to Merge Stones (opens in a new tab)
- Number of Ways to Wear Different Hats to Each Other (opens in a new tab)
Competitive programming
- AtCoder ABC 240 C (opens in a new tab)
- Codeforces 1739C (opens in a new tab)
- Codeforces 1765K (opens in a new tab)
- Codeforces 1547E (opens in a new tab)
- Codeforces 1678B2 (opens in a new tab)
- Codeforces 1677A (opens in a new tab)
- Codeforces 1633D (opens in a new tab)
- Codeforces 1398C (opens in a new tab)
- Codeforces 1741E (opens in a new tab)
- Codeforces 1740E (opens in a new tab)
- Codeforces 1787C (opens in a new tab)
- Codeforces 1703G (opens in a new tab)
- Codeforces 1061C (opens in a new tab)
- AtCoder ABC 248 C (opens in a new tab)
- AtCoder ABC 247 C (opens in a new tab)
- AtCoder ABC 242 C (opens in a new tab)
- Codeforces 1749D (opens in a new tab)
- Codeforces 895C (opens in a new tab)
- Codeforces 1778D (opens in a new tab)
- Codeforces 1954D (opens in a new tab)
- Codeforces 1799D1 (opens in a new tab)
- Codeforces 1716D (opens in a new tab)
If you want to know how to start competitive programming, read the roadmap from newbie to expert on Codeforces.