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/x1k8lxi5arrow-up-right

Longest Increasing Subsequence variants: https://leetcode.com/problems/longest-increasing-subsequence/arrow-up-right https://leetcode.com/problems/largest-divisible-subset/arrow-up-right https://leetcode.com/problems/russian-doll-envelopes/arrow-up-right https://leetcode.com/problems/maximum-length-of-pair-chain/arrow-up-right https://leetcode.com/problems/number-of-longest-increasing-subsequence/arrow-up-right https://leetcode.com/problems/delete-and-earn/arrow-up-right https://leetcode.com/problems/longest-string-chain/arrow-up-right

Partition Subset: https://leetcode.com/problems/partition-equal-subset-sum/arrow-up-right https://leetcode.com/problems/last-stone-weight-ii/arrow-up-right

BitMasking: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/arrow-up-right

Longest Common Subsequence Variant: https://leetcode.com/problems/longest-common-subsequence/arrow-up-right https://leetcode.com/problems/edit-distance/arrow-up-right https://leetcode.com/problems/distinct-subsequences/arrow-up-right https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/arrow-up-right

Palindrome: https://leetcode.com/problems/palindrome-partitioning-ii/arrow-up-right https://leetcode.com/problems/palindromic-substrings/arrow-up-right

Coin Change variant: https://leetcode.com/problems/coin-change/arrow-up-right https://leetcode.com/problems/coin-change-2/arrow-up-right https://leetcode.com/problems/combination-sum-iv/arrow-up-right https://leetcode.com/problems/perfect-squares/arrow-up-right https://leetcode.com/problems/minimum-cost-for-tickets/arrow-up-right

Matrix multiplication variant: https://leetcode.com/problems/minimum-score-triangulation-of-polygon/arrow-up-right https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/arrow-up-right https://leetcode.com/problems/burst-balloons/arrow-up-right

Matrix/2D Array: https://leetcode.com/problems/matrix-block-sum/arrow-up-right https://leetcode.com/problems/range-sum-query-2d-immutable/arrow-up-right https://leetcode.com/problems/dungeon-game/arrow-up-right https://leetcode.com/problems/triangle/arrow-up-right https://leetcode.com/problems/maximal-square/arrow-up-right https://leetcode.com/problems/minimum-falling-path-sum/arrow-up-right

Hash + DP: https://leetcode.com/problems/target-sum/arrow-up-right https://leetcode.com/problems/longest-arithmetic-sequence/arrow-up-right https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/arrow-up-right https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/arrow-up-right

State machine: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/arrow-up-right

Depth First Search + DP: https://leetcode.com/problems/out-of-boundary-paths/arrow-up-right https://leetcode.com/problems/knight-probability-in-chessboard/arrow-up-right

Minimax DP: https://leetcode.com/problems/predict-the-winner/arrow-up-right https://leetcode.com/problems/stone-game/arrow-up-right

Misc: https://leetcode.com/problems/greatest-sum-divisible-by-three/arrow-up-right https://leetcode.com/problems/decode-ways/arrow-up-right https://leetcode.com/problems/perfect-squares/arrow-up-right https://leetcode.com/problems/count-numbers-with-unique-digits/arrow-up-right https://leetcode.com/problems/longest-turbulent-subarray/arrow-up-right https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/arrow-up-right


Sample solutions for each of above problem type:

Longest Increasing Subsequence https://leetcode.com/problems/longest-increasing-subsequence/arrow-up-right https://leetcode.com/problems/largest-divisible-subset/arrow-up-right https://leetcode.com/problems/russian-doll-envelopes/arrow-up-right https://leetcode.com/problems/maximum-length-of-pair-chain/arrow-up-right https://leetcode.com/problems/number-of-longest-increasing-subsequence/arrow-up-right https://leetcode.com/problems/delete-and-earn/arrow-up-right https://leetcode.com/problems/longest-string-chain/arrow-up-right

Partition Subset Sum: https://leetcode.com/problems/partition-equal-subset-sum/arrow-up-right https://leetcode.com/problems/last-stone-weight-ii/arrow-up-right

BitMasking in DP: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/arrow-up-right

Longest Common Subsequence https://leetcode.com/problems/longest-common-subsequence/arrow-up-right https://leetcode.com/problems/edit-distance/arrow-up-right https://leetcode.com/problems/distinct-subsequences/arrow-up-right https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/arrow-up-right

Palindrome: https://leetcode.com/problems/palindrome-partitioning-ii/arrow-up-right https://leetcode.com/problems/palindromic-substrings/arrow-up-right

Coin Change: https://leetcode.com/problems/coin-change/arrow-up-right https://leetcode.com/problems/coin-change-2/arrow-up-right https://leetcode.com/problems/combination-sum-iv/arrow-up-right https://leetcode.com/problems/perfect-squares/arrow-up-right https://leetcode.com/problems/minimum-cost-for-tickets/arrow-up-right

Matrix multiplication: https://leetcode.com/problems/minimum-score-triangulation-of-polygon/arrow-up-right https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/arrow-up-right https://leetcode.com/problems/burst-balloons/arrow-up-right

Matrix/2D Array: https://leetcode.com/problems/matrix-block-sum/arrow-up-right https://leetcode.com/problems/range-sum-query-2d-immutable/arrow-up-right https://leetcode.com/problems/dungeon-game/arrow-up-right https://leetcode.com/problems/triangle/arrow-up-right https://leetcode.com/problems/maximal-square/arrow-up-right https://leetcode.com/problems/minimum-falling-path-sum/arrow-up-right

Hash + DP: https://leetcode.com/problems/target-sum/arrow-up-right https://leetcode.com/problems/longest-arithmetic-sequence/arrow-up-right https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/arrow-up-right https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/arrow-up-right

State machine: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/arrow-up-right https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/arrow-up-right

Depth First Search +DP: https://leetcode.com/problems/out-of-boundary-paths/arrow-up-right https://leetcode.com/problems/knight-probability-in-chessboard/arrow-up-right

Minimax DP: https://leetcode.com/problems/predict-the-winner/arrow-up-right https://leetcode.com/problems/stone-game/arrow-up-right

Miscellaneous: https://leetcode.com/problems/greatest-sum-divisible-by-three/arrow-up-right https://leetcode.com/problems/decode-ways/arrow-up-right https://leetcode.com/problems/count-numbers-with-unique-digits/arrow-up-right https://leetcode.com/problems/longest-turbulent-subarray/arrow-up-right https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/arrow-up-right

Please point out issues in solutions if you find any. There might be better approaches in few.

References:

Last updated