vThere are three ways to solve Fibonacci problem

  1. Recursion
  2. Memoize
  3. Bottom-up

'First Recursion approach:

def fib(n):
if n == or n == :
result =
else:
result = fib(n-) + fib(n -) return result;

As we can see to calculate fib(5), we need to calculate fib(3) twice and fib(2) three times.

Time complexity is O(2^n), because for each n from 3, we need to call fib() twice in else block:

else:
result = fib(n-) + fib(n -)

To solve the problem, we can use memoize solution to solve repeated calculation.

deb fib(n, memo):
if memo[n] != null
return memo[n]
if n == or n == :
result =
else:
result = fib(n - ) + fib(n-)
memo[n] = result
return result

Using fib(5) as example: to calulate fib(5) we need to know fib(4)..fib(3), fib(2), fib(1), since we already know fib(1), fib(2) = 1, then we can know fib(3) = 2..fib(4) = 3, fib(5) = 5.

Time complexity is O(2n + 1) -> O(n): because we just need to go though memo once. And 2*2 is because of:

result = fib(n - ) + fib(n-)

We still can improve it by using bottom up approach, because from the previous solution:

Using fib(5) as example: to calulate fib(5) we need to know fib(4)..fib(3), fib(2), fib(1), since we already know fib(1), fib(2) = 1, then we can know fib(3) = 2..fib(4) = 3, fib(5) = 5.

We can clear see the solution the problem by going from bottom (fib(1) & fib(2)) to up (fib(5)):

def fib_bottom_up(n):
if n == or n == :
return
bottom_up = new int[n+]
bottom_up[] =
bottom_up[] =
for i from upto n:
bottom_up[i] = bottom_up[i-]+bottom_up[i-] return bottom_up[n]

Time complexity is O(n).


Notice that some programming language has recursion limit, for example, python has set the limiation to 1000, which mean if you keep calling one function 1000 times, it will throw errors.

In this sense, bottom up is much better than recursion apporach (recursion and memoize).

[Algorithm] Fibonacci problem by using Dynamic programming的更多相关文章

  1. Dynamic Programming: Fibonacci

    Recently I watched an interesting video in youtube, the vbloger use calculating Fibonacci number to ...

  2. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  3. HDU-4972 A simple dynamic programming problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...

  4. 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

    动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...

  5. [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem

    Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...

  6. Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical

    http://julialang.org/ julia | source | downloads | docs | blog | community | teaching | publications ...

  7. [Optimization] Dynamic programming

    “就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...

  8. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

  9. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

随机推荐

  1. ASP.NET MVC 视图学习,纯干货

    最近用MVC专门为自己做了一个网站,用来记录文章心情和日记.加上和同事的一些交流感觉颇深.所以想把13年买的MVC 4高级编程重新看一遍,记录一些东西,以后应该用的到.视图总是被控制器渲染,因为控制器 ...

  2. Unable to find a qt build, to solve this problem specify a qt build

    可能路径设置不对,比如大小写错误导致找不到qmake编译器,点击VS工具栏的QT菜单,选择options,指定qt Build所在的路径(qt安装路径),然后点击ok. 这是修改过默认安装路径的

  3. zookeeper,hadoop安装部署其实与防火墙无关

    网上查看了很多人关于hadoop,zookeeper的文章,大多都把关闭防火墙作为首要前提,个人觉得这大可不必. 首先你需要知道你部署的是什么东西,它需要哪些端口即可.把相关端口打开就可以了啊.然后把 ...

  4. Spring学习的切入点

    spring是个顶级的框架,这话没毛病.很多人想把它征服,想去阅读它的源码,弄懂它的设计思想,从中学到精粹. 但是很多次打开后,看到庞大的体系结构,就懵逼了,不知从何入手. 我在这里总结下学习spri ...

  5. Flask实战第45天:完成前台登录界面

    我们的注册页面和登录页面有很多相似之处,因此,也可以基于一个模板来实现. 首先创建一个模板html,命名为front_signbase.html, 然后修改注册页面front_signup.html, ...

  6. java环境搭建的一些小知识

    1.Path配置的是可执行程序的路径,例如(java.exe.javac.exe),这些可执行程序一般在bin目录下,所以path一般配置的是在程序包的bin目录下,实现帮助操作系统操作java的目的 ...

  7. [Codeforces-div.1 494C] Helping People

    [Codeforces-div.1 494C] Helping People 试题分析 不难注意到题目所给的性质是一棵树,所以肯定是树形dp. 那么期望没有办法合并,我们还有一种最笨的方法就是求出概率 ...

  8. 【bfs+优先队列】POJ2049-Finding Nemo

    基本上算是普通但略有些繁琐的广搜.给出的墙面和门的坐标为点,而Nemo位于方格中. [思路] 首先思考一下如何存储下整个坐标系.我们预先约定,用一个方格的左下角顶点坐标来作为这个方格的坐标.map[i ...

  9. 1.3(SQL学习笔记)计算字段及函数

    一.计算字段 1.1拼接字段 一般情况下返回的字段是指定列的属性名.如果有时我们对返回格式有特殊要求. 例如,我们需要将显示商品名,即商品价格,同时商品名后面的价格放在括号内. prod_name(p ...

  10. AIM Tech Round (Div. 1) D. Birthday 数学 暴力

    D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...