Patterns in DP

Before starting the topic let me introduce myself. I am a Mobile Developer currently working in Warsaw and spending my free time for interview preparations. I started to prepare for interviews two years ago. At that time I should say I could not solve the two sum problem. Easy problems seemed to me like hard ones so most of the time I had to look at editorials and discuss section. Currently, I have solved ~800 problems and time to time participate in contests. I usually solve 3 problems in a contest and sometimes 4 problems. Ok, lets come back to the topic.

Recently I have concentrated my attention on Dynamic Programming cause its one of the hardest topics in an interview prep. After solving ~140 problems in DP I have noticed that there are few patterns that can be found in different problems. So I did a research on that and find the following topics. I will not give complete ways how to solve problems but these patterns may be helpful in solving DP.

Patterns

Minimum (Maximum) Path to Reach a Targetarrow-up-right Distinct Waysarrow-up-right Merging Intervalsarrow-up-right DP on Stringsarrow-up-right Decision Makingarrow-up-right

Minimum (Maximum) Path to Reach a Target


Problem list: https://leetcode.com/list/55ac4kucarrow-up-right

Generate problem statement for this pattern

Statement

Given a target find minimum (maximum) cost / path / sum to reach the target.

Approach

Choose minimum (maximum) path among all possible paths before the current state, then add value for the current state.

Generate optimal solutions for all values in the target and return the value for the target.

Top-Down

Bottom-Up

Similar Problems

746. Min Cost Climbing Stairsarrow-up-right Easy

Top-Down

Bottom-Up

64. Minimum Path Sumarrow-up-right Medium

Top-Down

Bottom-Up

322. Coin Changearrow-up-right Medium

Top-Down

Bottom-Up

931. Minimum Falling Path Sumarrow-up-right Medium

983. Minimum Cost For Ticketsarrow-up-right Medium

650. 2 Keys Keyboardarrow-up-right Medium

279. Perfect Squaresarrow-up-right Medium

1049. Last Stone Weight IIarrow-up-right Medium

120. Trianglearrow-up-right Medium

474. Ones and Zeroesarrow-up-right Medium

221. Maximal Squarearrow-up-right Medium

322. Coin Changearrow-up-right Medium

1240. Tiling a Rectangle with the Fewest Squaresarrow-up-right Hard

174. Dungeon Gamearrow-up-right Hard

871. Minimum Number of Refueling Stopsarrow-up-right Hard

Distinct Ways


Problem List: https://leetcode.com/list/55ajm50iarrow-up-right

Generate problem statement for this pattern

Statement

Given a target find a number of distinct ways to reach the target.

Approach

Sum all possible ways to reach the current state.

Generate sum for all values in the target and return the value for the target.

Top-Down

Bottom-Up

Similar Problems

70. Climbing Stairsarrow-up-right Easy

Top-Down

Bottom-Up

62. Unique Pathsarrow-up-right Medium

Top-Down

Bottom-Up

1155. Number of Dice Rolls With Target Sumarrow-up-right Medium

Note

Some questions point out the number of repetitions, in that case, add one more loop to simulate every repetition.

688. Knight Probability in Chessboardarrow-up-right Medium

494. Target Sumarrow-up-right Medium

377. Combination Sum IVarrow-up-right Medium

935. Knight Dialerarrow-up-right Medium

1223. Dice Roll Simulationarrow-up-right Medium

416. Partition Equal Subset Sumarrow-up-right Medium

808. Soup Servingsarrow-up-right Medium

790. Domino and Tromino Tilingarrow-up-right Medium

801. Minimum Swaps To Make Sequences Increasingarrow-up-right

673. Number of Longest Increasing Subsequencearrow-up-right Medium

63. Unique Paths IIarrow-up-right Medium

576. Out of Boundary Pathsarrow-up-right Medium

1269. Number of Ways to Stay in the Same Place After Some Stepsarrow-up-right Hard

1220. Count Vowels Permutationarrow-up-right Hard

Merging Intervals


Problem List: https://leetcode.com/list/55aj8s16arrow-up-right

Generate problem statement for this pattern

Statement

Given a set of numbers find an optimal solution for a problem considering the current number and the best you can get from the left and right sides.

Approach

Find all optimal solutions for every interval and return the best possible answer.

Get the best from the left and right sides and add a solution for the current position.

Top-Down

Bottom-Up

Similar Problems

1130. Minimum Cost Tree From Leaf Valuesarrow-up-right Medium

96. Unique Binary Search Treesarrow-up-right Medium

1039. Minimum Score Triangulation of Polygonarrow-up-right Medium

546. Remove Boxesarrow-up-right Medium

1000. Minimum Cost to Merge Stonesarrow-up-right Medium

312. Burst Balloonsarrow-up-right Hard

Top-Down

Bottom-Up

375. Guess Number Higher or Lower IIarrow-up-right Medium

DP on Strings

Problem List: https://leetcode.com/list/55afh7m7arrow-up-right

General problem statement for this pattern can vary but most of the time you are given two strings where lengths of those strings are not big

Statement

Given two strings s1 and s2, return some result.

Approach

Most of the problems on this pattern requires a solution that can be accepted in O(n^2) complexity.

If you are given one string s the approach may little vary

1143. Longest Common Subsequencearrow-up-right Medium

647. Palindromic Substringsarrow-up-right Medium

516. Longest Palindromic Subsequencearrow-up-right Medium

1092. Shortest Common Supersequencearrow-up-right Medium

72. Edit Distancearrow-up-right Hard

115. Distinct Subsequencesarrow-up-right Hard

712. Minimum ASCII Delete Sum for Two Stringsarrow-up-right Medium

5. Longest Palindromic Substringarrow-up-right Medium

Decision Making


Problem List: https://leetcode.com/list/55af7bu7arrow-up-right

The general problem statement for this pattern is forgiven situation decide whether to use or not to use the current state. So, the problem requires you to make a decision at a current state.

Statement

Given a set of values find an answer with an option to choose or ignore the current value.

Approach

If you decide to choose the current value use the previous result where the value was ignored; vice-versa, if you decide to ignore the current value use previous result where value was used.

198. House Robberarrow-up-right Easy

121. Best Time to Buy and Sell Stockarrow-up-right Easy

714. Best Time to Buy and Sell Stock with Transaction Feearrow-up-right Medium

309. Best Time to Buy and Sell Stock with Cooldownarrow-up-right Medium

123. Best Time to Buy and Sell Stock IIIarrow-up-right Hard

188. Best Time to Buy and Sell Stock IVarrow-up-right Hard

I hope these tips will be helpful 😊

Reference:

Last updated