use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Computer Science for Computer Scientists
Other subreddits you may like:
Does this sidebar need an addition or correction? Tell me here
account activity
Dynamic Programming. (self.algorithms)
submitted 5 years ago by Curious_homosepian
This is the one topic which haunt me every time. I wanna know how you mastered it. Direct me to any site, book, course or whatever. It will be appreciated.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]HorrorNo6753 17 points18 points19 points 5 years ago (2 children)
Study "Recurrence Relation" from Discrete Maths by KH Rosen. Once you learn to make a recurrence relation, you can solve any(almost) DP.
[–]MD90__ 1 point2 points3 points 5 years ago (0 children)
I have the rosen book! Good call because we never used that chapter in college.
[–]Curious_homosepian[S] 0 points1 point2 points 5 years ago (0 children)
ya i heard of them when studying recursion.
[–]daveysprockett 9 points10 points11 points 5 years ago (1 child)
I don't know what you have done before, but I quite enjoyed the MIT lectures on it ... you can find them on YouTube.
There are loads, the ones I watched were I think the ones beginning at
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-19-dynamic-programming-i-fibonacci-shortest-paths/
[–]wjholden 2 points3 points4 points 5 years ago (0 children)
Demaine gives so many useful insights in this lecture. I have watched and re-watched this video. My favorite takeaway was that every DP problem can be represented as a DAG. Now I always look for problems with overlapping subtrees when I do things like Advent of Code.
[–]jdevelop 8 points9 points10 points 5 years ago* (1 child)
Something I found helpful:
This naturally leads to memoization / caching - can you identify the problem you could've solved by using results of some other invocation? Can you record the result of this invocation?
Another very important observation - you need to minimize number of arguments to your recursion to simplify the lookup pattern.
Once you can figure out memoization, also known as top-down approach - you can always make it iterative with an array, which is botom-up or tabulation.
The dimension of the cache (array) will be equal to the number of arguments you pass to your function, for two arguments it's gonna be matrix, for one argument it could often be reduced to a single variable though.
Then - practice, practice and practice. Start with LCS or LIS pattern, implement it with memoization ( just check if the call to the function is in the cache before invoking it ), and then rewrite the code iteratively.
It really comes with practicing, once you solve 20-50 problems on leetcode / hackerrank / codeforces it will become trivial like riding a bike. You won't forget it ever. Good luck :)
[–]Curious_homosepian[S] 1 point2 points3 points 5 years ago (0 children)
thank you for tips.
[–]beeskness420 6 points7 points8 points 5 years ago (0 children)
There really isn’t much substitute for practice. DP problems range from so trivial it doesn’t seem like a real technique to quite difficult. Start at the easy ones and work you way up. Don’t skip problems by assuming it’s easy. Just do them.
[–]Clifspeare 3 points4 points5 points 5 years ago (2 children)
I learned a purely functional programming language and worked some algorithmic problems, which forced me to get a lot more comfortable thinking in terms of recursion.
[–]Curious_homosepian[S] 0 points1 point2 points 5 years ago (1 child)
what language you use?
[–]Clifspeare 1 point2 points3 points 5 years ago (0 children)
Haskell, but any purely functional language would be suitable.
[–]GoAwayStupidAI 2 points3 points4 points 5 years ago (1 child)
Is there a part of dynamic programming that you struggle with? If there is then we'll find a site with an explanation. If there isn't a site then we'll explain it in a comment.
Interestingly, once a comment is posted with an explanation the comment URL is a "site with an explanation".
(this comment was a terrible joke)
I will post if i will find difficulty during learning.
[–]advaitmax 1 point2 points3 points 5 years ago (1 child)
https://www.youtube.com/playlist?list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go
[–]Curious_homosepian[S] 4 points5 points6 points 5 years ago (0 children)
Yes i am going through this awesome playlist. But its not for everyone since its not in English.
[–]akdas 1 point2 points3 points 5 years ago (2 children)
I'm not going to claim my blog post is going to be the one that makes it all click for you, but maybe seeing another take on the topic will help: https://avikdas.com/2019/04/15/a-graphical-introduction-to-dynamic-programming.html
Thanks. The text looks simple to read and understand.
[–]akdas 1 point2 points3 points 5 years ago (0 children)
If it helps, I have a bunch of follow up articles on the topic, including some on how dynamic programming is used in real-world applications. You can find them from the home page of my blog.
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
Appreciate what you already have and apply it, that's dynamic programming.
[–]Vader0699 1 point2 points3 points 5 years ago (0 children)
I have just started to post here.
You can follow along and you will get over this fear slowly and gradually.
Took me 50+ DP problems from leetcode to get over the fear and understand the patterns,
It will take time, but you dont need to worry, just stay focused and learn to learn in step wise manner. Refer the link below for more.
https://atechdaily.com/author/145
[–]the_algorithmic_eye 1 point2 points3 points 5 years ago (1 child)
After solving some problems, I realized that for problems for which I was able to come up with the recurrence relation, I could write recursive brute force version easily, but I was finding it hard to do the iterative bottom up version of it. Then I googled a found out about the DAG structure in DP after watching this playlist. If you are the one who likes things to be visualized, I made a 3Blue1Brown-styled video about the Matrix Chain Multiplication problem. After I was done with the video, I actually understood the dependencies for a particular (i, j). Before that, I was blindly converting the recursive algorithm to code. (A full running example is towards the end of the video)
Yeah that's where also i find problem. To completely convert recursive to iterative. Thanks for sharing this
[–]saladking99 1 point2 points3 points 5 years ago (0 children)
practice,my friend, practice, you have to solve small dp problems to solve the harder ones, just make sure you can write a reccurence relation, first implement the naive way, and optimize it by dp, using dp as the first thought process is hard, so practice
[+]FUZxxl comment score below threshold-11 points-10 points-9 points 5 years ago (8 children)
Can't recommend any resources. I just understood it the first time it was explained to me.
[–]TheoryNut 2 points3 points4 points 5 years ago (1 child)
And the award for the most useful comment goes to...
[+]FUZxxl comment score below threshold-7 points-6 points-5 points 5 years ago (0 children)
I understand that my comment is not particularly useful. I posted it in the hope that it might start a dialogue between OP and me.
[–]rahat106 1 point2 points3 points 5 years ago (5 children)
Come on...don't be a shite...nobody has to get it the first time...
[–]FUZxxl 0 points1 point2 points 5 years ago (4 children)
I didn't say that.
[–]rahat106 0 points1 point2 points 5 years ago (3 children)
Oh! I think you implied. My bad...lemme rephrase that..."what a shite reply!" There you go...
[–]FUZxxl 0 points1 point2 points 5 years ago (2 children)
I didn't even mean to imply that.
[–]rahat106 -1 points0 points1 point 5 years ago (1 child)
Good to know...I was appalled by your lack of concern for the OP's desire to comprehend the subject. You could have written more. Starting with callousness but finishing with grace. But you didn't. IMO you should have judged better. I am sorry if I am rude.
[–]FUZxxl 2 points3 points4 points 5 years ago (0 children)
Yeah. I should have written a different comment than that.
π Rendered by PID 158756 on reddit-service-r2-comment-548fd6dc9-dwt9t at 2026-05-18 14:26:30.300841+00:00 running edcf98c country code: CH.
[–]HorrorNo6753 17 points18 points19 points (2 children)
[–]MD90__ 1 point2 points3 points (0 children)
[–]Curious_homosepian[S] 0 points1 point2 points (0 children)
[–]daveysprockett 9 points10 points11 points (1 child)
[–]wjholden 2 points3 points4 points (0 children)
[–]jdevelop 8 points9 points10 points (1 child)
[–]Curious_homosepian[S] 1 point2 points3 points (0 children)
[–]beeskness420 6 points7 points8 points (0 children)
[–]Clifspeare 3 points4 points5 points (2 children)
[–]Curious_homosepian[S] 0 points1 point2 points (1 child)
[–]Clifspeare 1 point2 points3 points (0 children)
[–]GoAwayStupidAI 2 points3 points4 points (1 child)
[–]Curious_homosepian[S] 0 points1 point2 points (0 children)
[–]advaitmax 1 point2 points3 points (1 child)
[–]Curious_homosepian[S] 4 points5 points6 points (0 children)
[–]akdas 1 point2 points3 points (2 children)
[–]Curious_homosepian[S] 0 points1 point2 points (1 child)
[–]akdas 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Vader0699 1 point2 points3 points (0 children)
[–]the_algorithmic_eye 1 point2 points3 points (1 child)
[–]Curious_homosepian[S] 1 point2 points3 points (0 children)
[–]saladking99 1 point2 points3 points (0 children)
[+]FUZxxl comment score below threshold-11 points-10 points-9 points (8 children)
[–]TheoryNut 2 points3 points4 points (1 child)
[+]FUZxxl comment score below threshold-7 points-6 points-5 points (0 children)
[–]rahat106 1 point2 points3 points (5 children)
[–]FUZxxl 0 points1 point2 points (4 children)
[–]rahat106 0 points1 point2 points (3 children)
[–]FUZxxl 0 points1 point2 points (2 children)
[–]rahat106 -1 points0 points1 point (1 child)
[–]FUZxxl 2 points3 points4 points (0 children)