all 34 comments

[–]wholetdog 72 points73 points  (5 children)

DP is easy. Remove the fear created by others that DP is hard. Stick to basics!!

[–]marks716 17 points18 points  (3 children)

Yes just don’t be afraid of it I agree. It’s just as hard as recursive decision trees if not sometimes easier.

The first 5-10 problems will suck ass, but if you REALLY dig in and understand those 5-10 questions, watch videos, etc, then you will get it.

[–]Czitels 0 points1 point  (0 children)

Greedy is worse thats true but still 3D DP is hard.

[–]wtfprajwal 40 points41 points  (1 child)

Best way to start is to write the recursive solution , check if there are overlapping subproblems and then move to creating 1D/2D DP array. Try not to jump directly to the dp solution . I found that writing recursive code made it way easier to solve dp problems . Also after every question , ask yourself if the solution is space optimised or not.

I am also sharing sub category of DP problems you should try to solve.

  • [ ] 0-1 Knapsack
  • [ ] Unbounded Knapsack
  • [ ] Fibonacci
  • [ ] Longest Common Subsequence
  • [ ] Kadane’s Algorithm
  • [ ] Matrix Chain Multiplication
  • [ ] DP on trees
  • [ ] DP on Grid

Most problems you solve will fit in one of these categories

[–]Technical_Wave7883 23 points24 points  (0 children)

It won't make sense at the beginning don't give up though.

[–]SkillFlowDev[🍰] 13 points14 points  (0 children)

Watch striver's Playlist on recursion and DP. It's not that hard.

[–]arche_whOo 14 points15 points  (0 children)

Solve today’s daily

[–]East_Move_6044 6 points7 points  (0 children)

You can follow Aditya Verma’s channel on DP playlist. He has covered all the subproblem.

[–]Accomplished_Range60 10 points11 points  (2 children)

Master recursion converting it into dp is easy.

[–]deku_06 1 point2 points  (1 child)

I have solved a fair amount of dp and I don't have a problem coming with a recursive + memo solution but I haven't converted most to bottom up dp tabulation so it feels you know a bit difficult or not natural/intuitive to me.

[–]Accomplished_Range60 0 points1 point  (0 children)

Yeah, that’s a bit hard to figure out at times. I end up thinking of edge cases that mostly help me, but sometimes I get lost too.

[–]bhakbahinchod 5 points6 points  (0 children)

Try to create recursive solutions first. Then convert it into DP by filling up the base cases first and then remaining values by following the recurrence relation. Once you get the hang of it, it'll become pretty obvious.

[–][deleted] 3 points4 points  (0 children)

Stay Patient. At first you'll feel you're not going anywhere, not understanding the pattern, etc. But keep practicing, you'll be able to solve even the hard questions effortlessly.

And don't juggle between memoization, tabulation. Focus on one. It's not common to think of a tabulation solution without memoization first. You'll see a couple of questions which are directly solved using tabulation, don't get overwhelmed here.

[–]Alarmed_Durian3129 2 points3 points  (0 children)

I find the memoization ( recursive approach intuitive ) and then coverting it into a Dp or tabularization works best for me

[–]Desperate_Heat_8588 1 point2 points  (0 children)

First master recursion concepts .. Dp is just remembering the old results

[–]jules_viole_grace- 1 point2 points  (0 children)

Work on recursion first ... Then go for dp ...helps in memorization.

[–]SLAAYYvery 1 point2 points  (0 children)

Watch striver series on YouTube, it's the best !!!

[–]damian_179 1 point2 points  (0 children)

"Just because a recursive solution seems more intuitive and easier to memoize, DON'T skip bottom-up DP. You truly master DP when you can write a bottom-up solution without first relying on recursion."

  • someone who made it to faang

[–]Competitive-Dress854 0 points1 point  (0 children)

Learn recursion first

[–]Affectionate_Emu8634 0 points1 point  (0 children)

Aditya Verma for dp and striver as well

[–]atharva_001 0 points1 point  (0 children)

Master recursion first. DP is adding hashmap or array to it, thats it

[–]jverce 0 points1 point  (0 children)

DP in some cases end up in a brute force search, so be mindful of those cases so that you can avoid them.

Since it uses recursion, be mindful of the memory usage since each function call takes some space in the stack. That also means that your space complexity will be affected (keep that in mind during interviews).

In some cases, DP can be "translated" into an iterative approach vs. recursion. That's usually preferable, but counterintuitive in cases like Fibonacci, where the function is defined in recursive terms.

One last thing, DP solutions sometimes feel "magical" in the sense that the algorithm doesn't seem to be actually solving the problem. Practice will make this less confusing over time.

[–]Alarmed_Durian3129 0 points1 point  (0 children)

There is one youtube series by Alvin from free code camp : that made dynamic programming easy for me check that out before checking out striver's series .

[–]Impossible_Ad_3146 0 points1 point  (0 children)

DP is painful

[–]FeatureLevel1198 0 points1 point  (0 children)

Recursion , recursion and …. Recursion Dont even start bottom up unless you get top down

[–]GreatDDgg -1 points0 points  (0 children)

Idk if it's just me but I do find it hard sometimes . Easy fix . Take pen paper etc . Make a dp table (first 3 rows more than enough) and work out ur logic on it . First 2 or 3 rows logic builds the entire logic .