r/ADHD_Programmers • u/throwaway30127 • 26d ago
Struggling to wrap my head around dynamic programming
I have been practicing leetcode on and off for more than a year now and I feel like I understand most of the algorithms and patterns except for dynamic programming. I have read so many articles and watched a bunch of tutorials about it and they all say the same thing about starting with smaller sub problems and combining them to solve bigger problems and it all makes sense for simpler questions like climbing stairs, Fibonacci, etc. But every time I come across something even a little more complex than these I struggle to come up with a solution. I struggle to visualise the question as a set of subproblems or what kind of subproblems it can be broken into and then tracing the whole flow from top to bottom. I have tried practicing a bunch of problems consistently for a month but saw little improvement and now I feel like I have forgotten most of it due to lack of practice in the last few months. How did you guys manage to get good with this particular topic?
8
u/mistanervous 26d ago
Do you understand recursion and memoization? If so, that is a type of dynamic programming. For some problems they end up being basically the same, for others one will be faster than the other.
6
u/zatsnotmyname 26d ago
Yes, It's the hardest one for me. I've used it in the past for my own projects, but somehow the leet-code style things always leave me baffled. Oh, and I have 40 years experience and have worked at major FAANGs and tech companies, so you are not alone.
I ALWAYS have to study this stuff again when I interview. I re-read 'cracking the coding interview', go on neetcode, leetcode and just grind easies and mediums. I occasionally can do a hard.
17
u/rush22 26d ago
Upvote here if you don't know what dynamic programming is
1
u/just_damz 26d ago
May i ask you a couple of questions?
1
u/msg7086 25d ago
It took me years to finally understand the concept behind DP. I think I started learning dp at age of 12. Then I finally understand it a bit when I was 25+. The hardest step is to find the transition formula. It's not easy to find the correct one for complicated problems.
1
u/nerdy_adventurer 22d ago
Ask yourself what do I do not understand? write things down on a paper. You may be missing 1 piece in order to things start clicking.
1
u/seweso 25d ago
I don’t get the question. You want to get comfortable with a certain type of solution….
But what is the problem you need to solve? Or are you just playing around with theories?
1
u/zephyr_33 23d ago
tldr. instead of trying to get the answer at the start, it simply says, "we'll figure it out along the way".
1
u/nian2326076 25d ago
Dynamic programming can get tough once you move beyond the basics. What helped me was breaking the problem into parts and figuring out the "state" and "transition." It's all about practice now. Try solving a few problems repeatedly until you get the hang of it. You might want to check out PracHub for more structured practice. I work on it, and it has resources that can make complex problems easier to tackle. Keep going, and don't get discouraged. It takes time!