Dynamic Programming
In this section, we will implement simple dynamic programming algorithm to solve fibonacci number.
We first define the function. It solves the subproblem start from 0 and incrementally solve the subproblem and finally return the result of the problem.
public static int fibonacci(int n){
int f[] = new int[n+1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
You can try to run the code here:
Run on jdoodle