[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 ...
随机推荐
- Python基础系列----字典、基本语句
1.定义 映 ...
- hdu 多校第一场
1001 思路:打表可以发现只有3|n 和 4|n 的情况有解,判一下就好啦. #include<bits/stdc++.h> #define LL long long #define f ...
- ubuntu 18.04下,KMS_6.9.1服务器启动后,客户端连接一段时间因为libnice而crash的问题修复
相应track issue: https://github.com/Kurento/bugtracker/issues/247 libnice crashes in socket code: g_so ...
- KMS使用CLion作为IDE来调试
KMS使用CLion作为IDE来调试,打开kms相应模块的目录,CLion自动识别相应的CMakeLists.txt. 然而会make失败,经搜索,看到Clion使用的自带的cmake,因此系统中安装 ...
- Windows下的cd命令
http://blog.sina.com.cn/s/blog_5e971b850100rtta.html 首先先了解下什么是驱动器,什么是盘符. 驱动器是指通过系统格式化并带有一个驱动器号的存储区域, ...
- 读书笔记(javascript 高级程序设计)
一. 数据类型: 1. undefined: 未声明和未初始化的变量,typeof 操作符返回的结果都是 undefined:(建议未初始化的变量进行显式赋值,这样当 typeof 返回 undefi ...
- Codeforces Round #300 Quasi Binary(DP)
Quasi Binary time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [BZOJ4859][BJOI2017]机动训练(DP)
4859: [BeiJing2017]机动训练 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 105 Solved: 63[Submit][Stat ...
- hdu 1402(FFT乘法 || NTT乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
如果某个位置i是Y,直接直到i+m-1为止填上新的数字. 如果是N,直接把a[i+m-1]填和a[i]相同即可,这样不影响其他段的答案. 当然如果前面没有过Y的话,都填上0就行了. #include& ...