This article has been cross-posted from here
Dynamic Programming is one of those techniques that every programmer should have in their toolbox. Bu...
For further actions, you may consider blocking this person and/or reporting abuse
You forgot that C++ has tail call optimization. This means you can run it in constant space:
this is similar to the for loop
You are right about the tail call optimisation. However, that would have beaten the purpose of gearing this article towards beginners and also keeping it language agnostic. The main issue I have had with other articles (and that I wanted to avoid here) is that either they skip intermediate steps or use tricks which are not conducive to understanding the technique itself.
You left in the
#define MOD 1000000007
and% MOD
which isn't part of the explanation. Though I recall see computing tests that have this requirement on them. :)I do mention the reason in the Top-Down Recursive approach with Memoization section. Re-iterating it here for your convenience We mod the result using 1000000007 to avoid overflows.
Oh, further optimization is possible. For example, with matrix exponents ;)
Haha. You got me there. I should have specified no further optimizations from a DP perspective :)
Another solution , probably can take a look at :
dev.to/wozaisuzhou/algorithms-chal...
Good article! I think C++ isn't the right language for it, though (syntactic noise, difficulty reproducing, the numbers are too large for it to handle).
Thanks Josh.
I feel that since it is a technique, it is not limited to a language. I guess it might be more accessible if it was in one of the newer languages. But, it's better to stick with a language you are comfortable with and I don't need to figure out a whole new benchmarking system :)
I could have written it in pseudocode but then the benchmarking would have been extremely difficult.
What would you suggest be done differently?
Wonderful post :-) Cleared up a lot of concepts for me.
Thanks Suraj.