Fibonacci series solved in a Dynamic programming - Tabulation approach.


package dp;

/**
 * Fibonacci series: 1, 1, 2, 3, 5, 8
 * 
 * Solved as Memoization technique.
 */
public class FibonacciDPTabulation {

public FibonacciDPTabulation() {
}

/**
* Function to find nth fibonacci number with tabular approach.
*/

int fib(int n) {
int[] fib = new int[n + 1];
int i = 0;
fib[0] = 1;
fib[1] = 1;

for (i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}

public static void main(String[] args) {
int n = 5;
System.out.println(new FibonacciDPTabulation().fib(n));
}


}

Comments

Popular posts from this blog

Java important topics list for interviews

Tough Sql practise questions for interviews involving SELECT queries - Part 2

DBMS important topics list for interviews

Program to find number lines and words in a given file

Tough Sql practise questions for interviews involving SELECT queries - Part 1

Producer Consumer Problem with Blocking Queue in Java.

Animation for String search - KMP Algorithm

Interactive Data Structure visualizations/animations for algorithms

Replace Occurances of the given string