Recently I watched an interesting video in youtube, the vbloger use calculating Fibonacci number to explain dynamic programming

after watch this video, I decide to write it down in English, also for practice my written English

ok, in this article, we will assume you already know what's Finabonacci number

commonly, we always use recursion to get the number, it's pretty easy to implement it

Recursion:

int Fib(int n)
{
if (n <= ) return ;
return Fib(n - ) + Fib(n - );
}

This is also the example when we learn recursion

the time complexity is O(X=2^n), it's calcualted like this

Fib(n) once

Fib(n -1) once

Fib(n-2) twice

Fib(n-3) Third

the total time is equal = 1+2+3...+(n - 1) = 2^n

this approach works for most of cases, but it's no effective and will cause stack over exception if the number is big, because there's call stacks

it cost really long time when I set n = 100

so we need to improve the recursion

we can see some numbers are calculated multiple times

for instance, Fib(5) = Fib(4) + Fib(3), Fib(4) = Fib(3) + Fib(2), Fib(3) will be calculated twice

Let's think about an approach to avoid it

Recursion and Memoize

in this appraoch, we will store the number when it's calculated

int Fib(int n, int[] memoized)
{
if (memoized[n] != ) return memoized[n];
if (n <= ) return ;
int f = Fib(n - ) + Fib(n - );
memoized[n] = f;
return f;
}

ok, we will only calculate once for one number, and the time complexity is O(n)

however there are still lots of call stack while calculating

the above 2 approaches are calculated from top to bottom, from n, n-1,...,1

How about calculate from bottom, just like the exmaple number see, 1,1,2,3,5,6...

Bottom Up

int Fib(int n)
{
if (n <= ) return ;
var memoized = new int[n + ];
memoized[] = ;
memoized[] = ;
for (int i = ; i <= n; i++)
{
memoized[i] = memoized[i - ] + memoized[i - ];
}
return memoized[n];
}

in this approach, we calcuate from bottom to up, altthough we add extra space for new array, but there are not so many call stacks, it's effective

The time complexity is also O(n)

ok, this is the summary of the video, I also found a video which explain dynamic programming by MIT

Please also find this video for reference

Dynamic Programming I: Fibonacci, Shortest Paths

Dynamic Programming: Fibonacci的更多相关文章

  1. 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 ...

  2. [Optimization] Dynamic programming

    “就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...

  3. 最优化问题 Optimization Problems & 动态规划 Dynamic Programming

    2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...

  4. 笔试算法题(44):简介 - 动态规划(Dynamic Programming)

    议题:动态规划(Dynamic Programming) 分析: DP主要用于解决包含重叠子问题(Overlapping Subproblems)的最优化问题,其基本策略是将原问题分解为相似的子问题, ...

  5. 五大常见算法策略之——动态规划策略(Dynamic Programming)

    Dynamic Programming   Dynamic Programming是五大常用算法策略之一,简称DP,译作中文是"动态规划",可就是这个听起来高大上的翻译坑苦了无数人 ...

  6. 动态规划(Dynamic Programming)算法与LC实例的理解

    动态规划(Dynamic Programming)算法与LC实例的理解 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Le ...

  7. 动态规划算法详解 Dynamic Programming

    博客出处: https://blog.csdn.net/u013309870/article/details/75193592 前言 最近在牛客网上做了几套公司的真题,发现有关动态规划(Dynamic ...

  8. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  9. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

随机推荐

  1. ubuntu更新出错--Could not get lock /var/lib/dpkg/lock

    ubuntu在vps上安装好后,通常第一个命令是更新系统软件.然而在运行的过程中,却出现这样的错误: E: Could not get lock /var/lib/dpkg/lock - open ( ...

  2. [svc]salt基本原理

    转载自:来自:http://tech.mainwise.cn/?p=438 说明:salt是一个异构平台基础设置管理工具(虽然我们通常只用在Linux上),使用轻量级的通讯器ZMQ,用Python写成 ...

  3. 如何利用dex2jar反编译APK

    工具/原料 电脑 dex2jar JD-GUI 方法/步骤 1 下载dex2jar和JD-GUI,在参考资料中添加了这两个工具的百度网盘下载地址供读者下载使用(笔者亲测) 2 找到我们准备测试用的ap ...

  4. Eclipse 最有用的快捷键

    Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...

  5. 集合运算 蓝桥杯 set容器

    题目描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的元素个数. 第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素. 第三行 ...

  6. Android——OnCreate

    OnCreate OnCreate是Android中的一个特别的函数,用来“表示一个窗口正在生成”.其不产生窗口,只是在窗口显示前设置窗口的属性如风格.位置等. 性    质 函数 特    点 不产 ...

  7. MyBatis增删改查模板

    1. 首先,和Spring整合一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  8. golang :连接数据库闲置断线的问题

    golang在进行数据库操作,一般来说我们使用Open函数创建一个数据库(操作)句柄:func Open(driverName, dataSourceName string) (*DB, error) ...

  9. 利用CSS生成精美细线Table表格

    精美的表格是前端开发用到的一个组件,很多时候我们利用复杂的页面style代码,来生成这样的表格,造成了页面的修改性和可读性都非常差.这里推荐直接使用css来产生一个细线表格. 使用方法也很简单: 第一 ...

  10. Linux Shell Vim 经常使用命令、使用技巧总结

    前言 本文总结了自己实际开发中的经常使用命令,不定时更新,方便自己和其它人查阅. 如有其它提高效率的使用技巧.欢迎留言. 本文地址 http://blog.csdn.net/never_cxb/art ...