[Algorithm] Fibonacci problem by using Dynamic programming
vThere are three ways to solve Fibonacci problem
- Recursion
- Memoize
- 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的更多相关文章
- Dynamic Programming: Fibonacci
Recently I watched an interesting video in youtube, the vbloger use calculating Fibonacci number to ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)
动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...
- [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 ...
- 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 ...
- [Optimization] Dynamic programming
“就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
随机推荐
- .NET 简单的递归使用场景
什么是递归:自己调用自己,直到满足条件跳出 递归的缺点: 递归很耗内存,容易让机器挂掉 比如递归文件夹,当文件夹的层级有非常非常多的时候,就很容易挂掉,因为递归的时候把上层文件夹的上下文都保存在内存中 ...
- 基于 Docker 的 DevOps 搭建
一直对 DevOps 感兴趣,最近心血来潮,搭乘 Docker 搞一搞,Let‘s Go !!! 1.拉取 GitLab docker pull gitlab/gitlab-ce 2.Run Git ...
- try catch finally 执行顺序面试题总结
在网上看到一些异常处理的面试题,试着总结一下,先看下面代码,把这个方法在main中进行调用打印返回结果,看看结果输出什么. public static int testBasic(){ int i = ...
- scrapy 安装出错 [err2] no such file or directory: 'README.rst'【已解决】
背景:pip安装一直不成功,很多模块用pip安装都不行,只好到github下载zip安装 c:\Document and settings\> python e:\scrapy-0.24\set ...
- ZSTU OJ 4273 玩具
枚举,二分,$RMQ$. 肯定是将连续一段中最大值免去花费,枚举起点之后,二分终点即可.可以证明单调性. #include<map> #include<set> #includ ...
- 在web项目中发布jaxws
概述 在web项目中发布基于jaxws的webservice. 参考文章:用JAX-WS在Tomcat中发布WebService 参考文章说,如果不是servlet3.0及以上,需要配置servlet ...
- Flask实战第65天:帖子按照发布时间和评论数量等排序
排序,我们需要在前端传递参数, 编辑front_index.html 编辑front.views.py from apps.models import HighlightPostModel from ...
- 【树形dp】VK Cup 2012 Round 1 D. Distance in Tree
统计树中长度为K的路径条数. 用f[u][k]表示从u结点的子树中出发,终止于u结点的长度为k的路径条数. 边dp边统计答案.为了防止重复统计,在枚举子节点的时候,先将该子节点和当前u结点(和前面已经 ...
- 给lnmp一键包中的nginx安装openresty的lua扩展
lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...
- Idea集成svn
Idea集成svn 既然要使用svn,首先需要下载一个 svn的客户端,到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows ...