Mark As Completed Discussion

Bottom-up Dynamic Programming

While the top-down approach uses memoization to optimize the recursive solution, the bottom-up approach uses tabulation to solve the Fibonacci sequence iteratively.

In the bottom-up approach, we start with the base cases (0 and 1) and calculate each Fibonacci number in a bottom-up manner by storing the previously calculated Fibonacci numbers in an array or a table. This allows us to build up the solution by iteratively calculating the Fibonacci numbers until we reach the desired number.

Here's an example of how to implement the bottom-up dynamic programming approach for the Fibonacci sequence in C#:

SNIPPET
1public class Fibonacci
2{
3    public static int GetFibonacci(int n)
4    {
5        if (n == 0)
6            return 0;
7        
8        if (n == 1)
9            return 1;
10        
11        int[] dp = new int[n + 1];
12        dp[0] = 0;
13        dp[1] = 1;
14        
15        for (int i = 2; i <= n; i++)
16        {
17            dp[i] = dp[i - 1] + dp[i - 2];
18        }
19        
20        return dp[n];
21    }
22}
23
24// Example usage:
25int fibonacciNumber = Fibonacci.GetFibonacci(5);
26Console.WriteLine(fibonacciNumber);
CSHARP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment