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. Interllij IDEA常用快捷键

    [常规] Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关 ...

  2. Docker与Android Studio的冲突问题

    在行业内,VisualBox.VMware.Hyper-V冲突也不是什么秘密了 我在公司的电脑上先安装了Docker,并在安装Docker之前在BIOS中开启了虚拟化支持,所以在启动Docker时没有 ...

  3. jQuery插件开发,jquery插件

    关于jQuery插件的开发自己也做了少许研究,自己也写过多个插件,在自己的团队了也分享过一次关于插件的课.开始的时候整觉的很复杂的代码,现在再次看的时候就清晰了许多.这里我把我自己总结出来的东西分享出 ...

  4. python的str,unicode对象的encode和decode方法(转)

    python的str,unicode对象的encode和decode方法(转) python的str,unicode对象的encode和decode方法 python中的str对象其实就是" ...

  5. django-BBS(1)

    1.首先分析BBS的设计需要,然后设计相应的数据库.填写在models.py 中 2.修改setting.py中的内容: a.将appname加入INSTALLED_APP中 b.修改DATABASE ...

  6. Struts2自定义转换器输入生日日期输出年、月、日、年龄

    BirthAction.java package com.action; import java.util.Calendar; import java.util.Map; import com.bea ...

  7. python 去掉换行符或者改为其他方式结尾的方法(end='')

    输入参数不换行,就是打印之后不换行,在python2.7中使用 "," >>>def test(): print 'hello', print "wor ...

  8. JZYZOJ1544 [haoi2016T2]放棋子 错排公式 组合数学 高精度

    http://172.20.6.3/Problem_Show.asp?ID=1544&a=ProbNF 看了题解才意识到原题有错排的性质(开始根本不知道错排是什么). 十本不同的书放在书架上. ...

  9. AGC 022 B - GCD Sequence

    题面在这里! 锻炼脑子的小构造题... 一开始被 a[]<=30000 且 序列 gcd = 1所困扰,但是发现这并没有什么,因为我接下来发现了一种总是能构造出 序列和是6的倍数的方案. 首先如 ...

  10. AGC 016 C - +/- Rectangle

    题面在这里! 结合了贪心的构造真是妙啊2333 一开始推式子发现 权是被多少个w*h矩形覆盖到的时候 带权和 <0 ,权都是1的时候带权和 >0,也就是说被矩形覆盖的多的我们要让它尽量小. ...