r/leetcode
•
u/hnlasd12
•
Jul 01 '23
•
1
1
The Ultimate Dynamic Programming Roadmap
Hey guys, I've seen a lot of discussions about how to study DP in this subreddit. We went through a lot of (almost all) DP problems on leetcode and came up a study list here. I think it pretty much covers all the patterns necessary for leetcode. What's special about the list 1) goes from simpler to more complex patterns 2) categorized by state transition (explained in the video walkthrough) so if you solve the first problem in a pattern you can use a similar state transition to solve others in the list.
Here's the list and a 1.5 hour video walking through each pattern and solving a question for each pattern: https://www.youtube.com/watch?v=9k31KcQmS_U
EDIT: here's the updated list! https://algo.monster/dp
Hope it's helpful to you!
Group 1 (warmup):
Basic questions to get a feel of DP.
- https://leetcode.com/problems/climbing-stairs/ in linear time
- https://leetcode.com/problems/n-th-tribonacci-number/ in linear time
- https://leetcode.com/problems/perfect-squares/ (maybe)
Group 2 (linear sequence, linear time, constant transition):
Dp solution requires us to solve the sub problem on every prefix of the array. A prefix of the array is a subarray from 0 to i for some i.
- https://leetcode.com/problems/min-cost-climbing-stairs/ in linear time
- https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
- https://leetcode.com/problems/house-robber/
- https://leetcode.com/problems/decode-ways/
- https://leetcode.com/problems/minimum-cost-for-tickets/
- https://leetcode.com/problems/solving-questions-with-brainpower/
Group 3 (on grids):
Dp table will have the same dimensions as grid, the state at cell i,j will be related to the grid at cell i,j.
- https://leetcode.com/problems/unique-paths/
- https://leetcode.com/problems/unique-paths-ii/
- https://leetcode.com/problems/minimum-path-sum/
- https://leetcode.com/problems/count-square-submatrices-with-all-ones/
- https://leetcode.com/problems/maximal-square/
- https://leetcode.com/problems/dungeon-game/
Group 4 (two sequences, O(NM) style):
Dp[i][j] is some value related to the problem solved on prefix of sequence 1 with length i, and prefix on sequence 2 with length j.
- https://leetcode.com/problems/longest-common-subsequence/
- https://leetcode.com/problems/uncrossed-lines/ (longest common subsequence)
- https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/
- https://leetcode.com/problems/edit-distance/
- https://leetcode.com/problems/distinct-subsequences/
- https://leetcode.com/problems/shortest-common-supersequence/
Group 5 (Interval dp):
Dp problem is solved on every single interval (subarray) of the array
- https://leetcode.com/problems/longest-palindromic-subsequence/
- https://leetcode.com/problems/stone-game-vii/
- https://leetcode.com/problems/palindromic-substrings/
- https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ (hard to see interval)
- https://leetcode.com/problems/strange-printer/ (hard)
- https://leetcode.com/problems/burst-balloons/ (hard)
Group 6 (linear sequence transition like N2 Longest Increasing Subsequence)
Dp problem is solved on every prefix of the array. Transition is from every index j < i.
- https://leetcode.com/problems/count-number-of-teams/
- https://leetcode.com/problems/longest-increasing-subsequence/
- https://leetcode.com/problems/partition-array-for-maximum-sum/
- https://leetcode.com/problems/largest-sum-of-averages/
- https://leetcode.com/problems/filling-bookcase-shelves/
Group 7 (knapsack-like)
Dp state is similar to the classical knapsack problem.
- https://leetcode.com/problems/partition-equal-subset-sum/
- https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
- https://leetcode.com/problems/combination-sum-iv/
- https://leetcode.com/problems/ones-and-zeroes/
- https://leetcode.com/problems/coin-change/
- https://leetcode.com/problems/coin-change-ii/
- https://leetcode.com/problems/target-sum/
- https://leetcode.com/problems/last-stone-weight-ii/
- https://leetcode.com/problems/profitable-schemes/ (hard)
Group 8 (topological sort with graphs. advanced, optional)
Solve dp on all subgraphs that are connected to each node
- https://leetcode.com/problems/longest-string-chain/
- https://leetcode.com/problems/longest-increasing-path-in-a-matrix/
- https://leetcode.com/problems/course-schedule-iii/
Group 9 (dp on trees. advanced, optional)
Solve dp problem on all subtrees.
Also get the list here: https://algo.monster/dp
36
u/Nis_0208 Jul 01 '23
OP you are a legend thank you for this! (what's up with that font size though xD)
15
u/kerfluffle99 Jul 01 '23
amazing! tysm op!!!!
8
u/chillblaze Jul 01 '23
Thanks. Do you think it is suboptimal for DP if you go with a purely Bottom Up Tabulation approach?
For me, Top Down Memo never really clicked...
3
u/AlwaysHuangry <T260> <E69> <M182> <H9> Jul 01 '23
Whoa. This looks promising. Nice work!
3
u/tempo0209 Jul 02 '23
Hey op and to anyone reading this, a dumb question for you, I know solving question by pattern is a way to go for most people like me who are starting out, this definitely looks promising for me! and thank you! But, having said that is it safe to assume the person who is attempting to solve say "climbing stairs" is supposed to be aware that this particular problem needs to be solved using DP? meaning if I wasn't aware that a particular problem needs a DP solution, how would you suggest to develop that intuition by looking at the problem? I have heard about
optimal substructure, andoverlapping subproblemsso should i invest time in first figuring out if the said problem has these properties and then try a DP approach? sorry if my question is confusing.16
u/RosieYap Sep 04 '23
3 weeks of work and I finally finished up till Group 3. So far I must say it is a pretty targeted list which has helped me loads in grasping each concept. Finally managed to solve my first Medium question without looking at any solution. Thanks for this list!
6
u/flexr123 Jul 01 '23
Nice list. Though I'd like to see more DP on tree (rerooting), Bitmask DP and Digit DP too.
4
u/s-nj33v Jul 03 '23
OP do you have Backtracking resources like this?
1
u/Livinglifepeacefully Jul 01 '23
When you said it goes from simple to complex, did you mean group wise? Or within a group?
1
u/NamoKaul Jul 02 '23
Can you do other data structures too if possible? Wonderful work BTW 💯👏
1
u/MantiS_praying Jul 02 '23
Ty so much man!
1
u/Churglish Jul 02 '23
Dynamic programming right now is a huge weakness for me. Thanks for this!
3
u/Scared-Dot3177 Mar 10 '24
All the above problems are added to this list https://leetcode.com/list/pckvxpxs
2
u/selfzoned_me May 12 '24
Can anyone tell how to do the https://leetcode.com/problems/minimum-time-to-make-rope-colorful/ problem mentioned above with DP approach. I could figure out Greedy approach for it. I tried looking into solutions section but could not find a simple enough DP solution.
2
u/Viva_Nova Aug 31 '25 edited Aug 31 '25
Amazing u/hnlasd12 Went through all these problems in order and I feel like I mastered DP which is the hardest category in my opinion. Do you have something similar for the others (graphs, sliding window, etc)?
2
u/VKrishna04 May 06 '26
why is such a beautiful roadmap removed if anyone want they can access the same via algo . monster/problems/dp-list
2
u/quadzilla225 May 06 '26
why exactly was this removed? are the mods of this sub antieducation or something?
1
u/PaintingLivid6725 Jan 19 '25
Hi great list!! Thanks! Small suggestion, group 3 problem 4 and 5 are exactly the same, you can replace one maybe.
1
u/ProfessionalKey5810 Jun 15 '25
Are there solutions anywhere for these?
1
u/OrganizationOnly8016 Jun 28 '25
Hey, OP. I was using your list to solve the questions and going through the comments just to get an understanding of where the rest of the learners are, and it struck me - how long have you been learning to get to this level of mastery? is solving such problems very intuitive to you? how many years would you say it would take somebody to get there?
I can definitely feel myself getting better but I feel like I'm nowhere near being able to solve questions in interviews. I have been able to guess the logic to an extent, but I keep missing the tinier details, which effectively leads to not being able to solve the question on my own. I've only ever solved a question on my own once in a while.
I, ofc, don't expect to get better overnight, but I just want to hear your experience, if you're okay with sharing.
1
u/No_Entrepreneur_8035 Jul 08 '25
ily
1
u/ibringallthedramama Jul 23 '25
in that video, i only wished for one thing which is for the other guy to not interrupt the one that's coding but yeah, wonderful resource!
1
u/Next_Complex5590 Knight Mar 21 '26
May god bless you
1
u/VastAshamed4618 May 09 '26
why this was removed
1
u/TheBoyWhoLivez Jul 01 '23
Yes
1
u/Vaulteroni Jul 02 '23
Can you please make the course free for a weekend?
1
u/the_fuckin_nobody Jul 02 '23
Thanks OP!
1
u/Gintoki-desu Sep 04 '23
Thanks for sharing!
1
u/Just-Ad3390 Sep 23 '23
Do you have anything like this for Trees and graphs
1
u/Zqin Dec 07 '23
I love you, thank you for this
1
u/fleetingleaf Dec 12 '23
This is helpful, thanks.
1
u/Safe_Buy_4434 Jun 12 '26
0
u/Sick_Kebab Jul 01 '23
.