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
Post a Comment