How do you know what to memoize in top-down Dynamic Programming problems?
I understand it is to do with capturing the permutations of state. E.g. in the Fibonacci numbers question, it’s memoizing the Fibonacci sequence fib(8) = 21
. I understand that; it’s a pure function and input results to output.
However, with more difficult Dynamic Programming problems, what to memoize isn’t that obvious, because it seems to me that it usually requires the previous state.
For example, for 474. Ones and Zeros:
https://leetcode.com/problems/ones-and-zeroes/description/
You should memoize the m
and n
and current index
.
But surely at any given index and m and n, it would be different depending on the previous values (it gets all added together)? Say you are on index 300, with m being 50 and n being 20, the memoized answer will really depend on the previous value
To me it’s like saying I want to get to Starbucks, but oh, you are facing left, so you should turn right and go forward. This is not true, as you’d need to know your accumulated current position
Please advise.