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. .net core web api 获取request body的纯文本

    本文代码 https://github.com/wuhaibo/readPlainTextDotNetCoreWepApi 总有些时候我们希望获得Request body 的纯文本 那么怎么做呢?很简 ...

  2. Web测试中容易被忽略的Charset问题

    今天继续进行一个更综合的脚本制作,录制设置.进行录制.脚本修改,一切都轻车熟路,进行得很顺利.经过近一个小时的对比和修改,OK,脚本大功告成,终于可以小试牛刀了,嘿嘿.    运行,replay lo ...

  3. lr计算程序执行消耗时间的比较:

    去除程序执行的两种方式: 1.通过一个事务:在需要消除的代码段,使用lr_wasted_time(wasteTime); querySubmit() { char newStr4[10000]=&qu ...

  4. cookie使用和销毁

    一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网 ...

  5. vue v-bind绑定属性和样式

    这期跟大家分享的,是v-bind指令.它可以往元素的属性中绑定数据,也可以动态地根据数据为元素绑定不同的样式. 绑定属性 最简单的例子,我们有一张图片,需要定义图片的src.我们可以直接在元素的属性里 ...

  6. javascript中的this总结

    1.关于this 我们需要根据 "调用位置" 上函数的 "调用方式" 来确定函数中this使用的 "绑定规则" 2.绑定规则 非严格模式下: ...

  7. 【POJ 2186】Popular Cows

    http://poj.org/problem?id=2186 tarjan求强连通分量. 因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了. 写手动栈时思路清晰一点应该是不会 ...

  8. [NOI 2011][BZOJ 2434] 阿狸的打字机

    传送门 AC自动机 + 树状数组 建成AC自动机后,设end[i]为第i个串的末尾在Trie树上的节点. 可以发现,对于一个询问(x,y),ans等于Trie树上root到end[y]这条链上fail ...

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

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

  10. java笔记之面向对象

    一.面向过程与面向对象的区别 1 面向过程:主要关注点是:实现的具体过程,因果关系[集成显卡的开发思路] * 优点:对于业务逻辑比较简单的程序,可以达到快速开发,前期投入成本较低. * 缺点:采用面向 ...