Explain this dynamic programming climbing n-stair code

北城余情 提交于 2019-11-29 01:36:36

Well, first you need to understand the recursive formula, and how we derived the iterative one from it.

The recursive formula is:

f(n) = f(n-1) + f(n-2)
f(0) = f(1) = 1

(f(n-1) for one step, f(n-2) for two steps, and the total numbers is the number of ways to use one of these options - thus the summation).

If you look carefully - this is also a well known series - the fibonacci numbers, and the solution is simply calculating each number buttom-up instead of re-calculating the recursion over and over again, resulting in much more efficient solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!