DP for Beginners [Problems | Patterns | Sample Solutions]
As some folks requested to list down good Dynamic Programming problems to start practice with. So, I am listing down them below and dividing them into different DP problem pattern. Problem listed in group follow a particular pattern and similar approach to solve them. I have tried to list those which till now I have solved, might have missed a few as well. Mostly are LC Medium problems and few LC Hard ones. Beginner folks can wish to look at solutions listed below of particular pattern and can try to solve the other problem themselves.
Full problem list: https://leetcode.com/list/x1k8lxi5
Longest Increasing Subsequence variants: https://leetcode.com/problems/longest-increasing-subsequence/ https://leetcode.com/problems/largest-divisible-subset/ https://leetcode.com/problems/russian-doll-envelopes/ https://leetcode.com/problems/maximum-length-of-pair-chain/ https://leetcode.com/problems/number-of-longest-increasing-subsequence/ https://leetcode.com/problems/delete-and-earn/ https://leetcode.com/problems/longest-string-chain/
Partition Subset: https://leetcode.com/problems/partition-equal-subset-sum/ https://leetcode.com/problems/last-stone-weight-ii/
BitMasking: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
Longest Common Subsequence Variant: https://leetcode.com/problems/longest-common-subsequence/ https://leetcode.com/problems/edit-distance/ https://leetcode.com/problems/distinct-subsequences/ https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
Palindrome: https://leetcode.com/problems/palindrome-partitioning-ii/ https://leetcode.com/problems/palindromic-substrings/
Coin Change variant: https://leetcode.com/problems/coin-change/ https://leetcode.com/problems/coin-change-2/ https://leetcode.com/problems/combination-sum-iv/ https://leetcode.com/problems/perfect-squares/ https://leetcode.com/problems/minimum-cost-for-tickets/
Matrix multiplication variant: https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ https://leetcode.com/problems/burst-balloons/
Matrix/2D Array: https://leetcode.com/problems/matrix-block-sum/ https://leetcode.com/problems/range-sum-query-2d-immutable/ https://leetcode.com/problems/dungeon-game/ https://leetcode.com/problems/triangle/ https://leetcode.com/problems/maximal-square/ https://leetcode.com/problems/minimum-falling-path-sum/
Hash + DP: https://leetcode.com/problems/target-sum/ https://leetcode.com/problems/longest-arithmetic-sequence/ https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/ https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
State machine: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
Depth First Search + DP: https://leetcode.com/problems/out-of-boundary-paths/ https://leetcode.com/problems/knight-probability-in-chessboard/
Minimax DP: https://leetcode.com/problems/predict-the-winner/ https://leetcode.com/problems/stone-game/
Misc: https://leetcode.com/problems/greatest-sum-divisible-by-three/ https://leetcode.com/problems/decode-ways/ https://leetcode.com/problems/perfect-squares/ https://leetcode.com/problems/count-numbers-with-unique-digits/ https://leetcode.com/problems/longest-turbulent-subarray/ https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
Sample solutions for each of above problem type:
Longest Increasing Subsequence https://leetcode.com/problems/longest-increasing-subsequence/ https://leetcode.com/problems/largest-divisible-subset/ https://leetcode.com/problems/russian-doll-envelopes/ https://leetcode.com/problems/maximum-length-of-pair-chain/ https://leetcode.com/problems/number-of-longest-increasing-subsequence/ https://leetcode.com/problems/delete-and-earn/ https://leetcode.com/problems/longest-string-chain/
Partition Subset Sum: https://leetcode.com/problems/partition-equal-subset-sum/ https://leetcode.com/problems/last-stone-weight-ii/
BitMasking in DP: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
Longest Common Subsequence https://leetcode.com/problems/longest-common-subsequence/ https://leetcode.com/problems/edit-distance/ https://leetcode.com/problems/distinct-subsequences/ https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
Palindrome: https://leetcode.com/problems/palindrome-partitioning-ii/ https://leetcode.com/problems/palindromic-substrings/
Coin Change: https://leetcode.com/problems/coin-change/ https://leetcode.com/problems/coin-change-2/ https://leetcode.com/problems/combination-sum-iv/ https://leetcode.com/problems/perfect-squares/ https://leetcode.com/problems/minimum-cost-for-tickets/
Matrix multiplication: https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ https://leetcode.com/problems/burst-balloons/
Matrix/2D Array: https://leetcode.com/problems/matrix-block-sum/ https://leetcode.com/problems/range-sum-query-2d-immutable/ https://leetcode.com/problems/dungeon-game/ https://leetcode.com/problems/triangle/ https://leetcode.com/problems/maximal-square/ https://leetcode.com/problems/minimum-falling-path-sum/
Hash + DP: https://leetcode.com/problems/target-sum/ https://leetcode.com/problems/longest-arithmetic-sequence/ https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/ https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/
State machine: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
Depth First Search +DP: https://leetcode.com/problems/out-of-boundary-paths/ https://leetcode.com/problems/knight-probability-in-chessboard/
Minimax DP: https://leetcode.com/problems/predict-the-winner/ https://leetcode.com/problems/stone-game/
Miscellaneous: https://leetcode.com/problems/greatest-sum-divisible-by-three/ https://leetcode.com/problems/decode-ways/ https://leetcode.com/problems/count-numbers-with-unique-digits/ https://leetcode.com/problems/longest-turbulent-subarray/ https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
Please point out issues in solutions if you find any. There might be better approaches in few.
References:
Last updated