Hacker News new | ask | show | jobs
by tikhonj 2656 days ago
The tricky thing with dynamic programming is realizing that it fits—you have to understand how the problem you're solving decomposes into overlapping subproblems that are worth caching. Once you know what the subproblems are, implementing the algorithm is mostly a matter of getting some fiddly indexing logic right.

Take string edit distance for example: the key to the dynamic programming problem is seeing how the exponential blow up of possibilities actually has a rich overlapping structure that you can cache in a table. If you didn't know string edit distance was amenable to dynamic programming, you might not realize it at all even if you are constantly thinking about caching results. Once you see the structure, the algorithm becomes relatively straightforward.

1 comments

Absolutely, but I attribute the ability to decompose a problem into overlapping subproblems as an understanding of recursion, so I had implicitly assumed such an understanding.
Try solving some difficult DP problems on CodeForces. You will find that an "understanding of recursion" is not enough to solve them.
No, no, it is an understanding of how to recurse just right. Not an understanding of the concept of recursion in general.

Basically, recursion plus memoization is almost the same in terms of power and approach as dynamic programming.

No, it's not, and if you actually tried to solve one of the harder DP problems, this would become very obvious to you.

Here's one for you: https://codeforces.com/problemset/problem/1097/G

It's a dp problem, and you understand how to recurse just right, and you understand memoization, so you'll be able to solve it, right? Link your solution when you reply please.

...That's what I thought.
Behaving like an asshole and not getting a response is not proof of anything other than people not being interested in engaging with assholes.