Dynamic Fibonacci

If we want to understand the true essence of dynamic programming, we should consider the simple example of fibonacci series. Using the conventional algorithm the time complexity of the solution is O(2^n).

 def fibonacci(n):
    i, a, b = 1, 0, 1
         a, b = b, a+b
         i += 1
    return b

But using the dynamic programming we can reduce the function call, as well as the time complexity to O(n).

class Main {
 int[] f = new int[100];
 public int fibonacci(int n) {
       f[0] = f[1] = 1;
       for(int i=2;i<=n;i++) {
            f[i] = f[i-1] + f[i-2];
 return f[n];

Author: ashiquechowdhury

A software developer, and a data science enthusiast

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s