[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 ...
随机推荐
- 本人博客已经搬至SegmentFault.com
本人博客已经搬至SegmentFault.com 具体链接:http://segmentfault.com/blog/zhoutk
- UVALive - 7042 The Problem to Make You Happy 博弈
题目大意:给你一个有向图, Bob 和 Alice 在做游戏,每轮他们走一步,当Bob 和 Alice在同一个点或者 Bob无路可走,Bob输,否则Alice输. 思路:因为在Bob赢的时候存在有环的 ...
- Centos7yum安装LNMP
(1)安装nginx 0.关闭防火墙 systemctl stop firewald.service systemctl disable firewald.service 1.使用nginx官方提供的 ...
- struts2核心配置之Result
result作用:在struts.xml中,使用<result>元素配置result逻辑视图和物理视图之间的映射 元素属性 属性 说明 是否必须 name 指定逻辑视图的名称(Action ...
- gwy常识
其实公务员考试是一门艺术,七分靠水平,三分凭发挥,充分而又细致的准备则是取得优秀成绩的前提.考生若想在笔试中成功上岸,还需苦练内功,凭技巧和真才实学在考场上一较高下.那么针对历年上海公务员考试笔试考情 ...
- Maven环境配置及idea建Maven工程
https://blog.csdn.net/qq_37497322/article/details/78988378
- 【UOJ #279】【UTR #2】题目交流通道
http://uoj.ac/problem/279 先判断答案为0的情况,\(d(i,i)\neq 0\),\(d(i,j)\neq d(j,i)\),\(d(i,j)>d(i,k)+d(k,j ...
- luogu P2254 [NOI2005]瑰丽华尔兹
题目链接 luogu P2254 [NOI2005]瑰丽华尔兹 题解 为什么我我我不放放放bzoj的链接呢? 因为打的暴力啊,然后bzojT了呀QAQQQQQ(逃 然后luogu竟然过了呀呀呀 dp[ ...
- BZOJ 2049 [Sdoi2008]Cave 洞穴勘测(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题目大意] 要求支持树的断边和连边,以及连接查询 [题解] LCT练习题 [代 ...
- Java编程思想学习(四)----第四章:控制执行流程
在Java中,涉及的关键字包括if-else.while.do-while.for.return.break 以及选择语句switch.然而,Java并不支持goto语句(该语句引起许多反对意见,但它 ...