How to implement Fibonacci using bottom-up approach using C#?

CsharpServer Side ProgrammingProgramming

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. The bottom-up approach first focuses on solving the smaller problems at the fundamental level and then integrating them into a whole and complete solution.

Time complexity − O(N)

Space complexity − O(N)

Example

public class DynamicProgramming{
public int fibonacciBottomupApproach(int n){
int[] dpArr = new int[150];
dpArr[1] = 1;
for (int i = 2; i <= n; i++){
dpArr[i] = dpArr[i - 1] + dpArr[i - 2];
}
return dpArr[n];
}
}

static void Main(string[] args){
DynamicProgramming dp = new DynamicProgramming();
Console.WriteLine(dp.fibonacciBottomupApproach(5));
}

Output

5
Published on 17-Aug-2021 08:14:58