Let's now write the tabulation approach for solving the Fibonacci question.
Now for this we will need a DP array.
So let me say let DP is equal to mu array.
And this array will have the values going from zero up to n.
So therefore the size of this array is going to be n plus one okay.
And we will fill initially each cell in this array with the value zero okay.
Now what we will do is we will fill the position zero or the index zero in the DP array with the value
zero.
So I can say dp at zero is equal to zero.
And if n is greater than zero, then I will fill dp at one to be equal to one.
Okay, so if n is greater than zero we can say dp at one is equal to one.
Now why do I have this condition over here.
Like if n is equal to zero.
And if I try to access dp at one over here, then it will fail, right?
Because if n is equal to zero, then the dp array would just have one element, or its size would be
just one and there would be no DP at one.
So that's why I have this check over here.
So if n is greater than zero, then we set dp at one to be equal to one.
After that we will need a count variable.
So let us say let count is equal to one and we will have a while loop that goes on as long as count
is less than n, and when count is equal to n, we will come out of the while loop and then we will
just return depee at n.
So this is how we are going to approach this.
And inside the while loop over here, we will just keep calculating the next value in the DP array.
So we will say count plus is equal to one.
So we incrementing count.
So as of now let us say the value of n is three for example.
So we know dp at zero and dp at one.
And we have set count to equal one because we know the value up to one which is this one over here.
And over here we are incrementing count.
So count becomes equal to two.
And we will say dp at count, which for the example that I was discussing is dp at two.
And that would be dp at count minus one plus dp at count minus two.
Because that's the Fibonacci property right?
FIB n is equal to five n minus one plus fib n minus two.
So we are calculating dp at count.
And then again we come over here.
So if count is less than n we again increment count.
And we calculate the new dp of count.
And when count becomes equal to n we come out of this.
So that would mean that we have already calculated dp at n.
And then we come over here and we return dp at n.
So this is the tabulation approach for solving this question.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
Now again let me remind you do make use of the user logs which are provided for each test case in case
you are stuck at some point and you want some help to debug your code.