Complete the code to initialize the base case for Fibonacci.
int[] fib = new int[n + 1]; fib[0] = [1];
The Fibonacci sequence starts with 0 at index 0.
Complete the code to initialize the second base case for Fibonacci.
fib[1] = [1];
The second Fibonacci number is 1 at index 1.
Fix the error in the loop to correctly compute Fibonacci numbers.
for (int i = 2; i <= n; i++) { fib[i] = fib[i - 1] [1] fib[i - 2]; }
Each Fibonacci number is the sum of the two previous numbers.
Fill both blanks to complete the method that returns the nth Fibonacci number using DP.
public int Fibonacci(int n) {
int[] fib = new int[n + 1];
fib[0] = 0;
fib[1] = 1;
for (int i = [1]; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - [2]];
}
return fib[n];
}The loop starts at 2 because fib[0] and fib[1] are base cases. The second blank is 2 to get fib[i-2].
Fill all three blanks to create a dictionary that maps each index to its Fibonacci number for n, filtering only those greater than 0.
var fibDict = new Dictionary<int, int>(); int[] fib = new int[n + 1]; fib[0] = 0; fib[1] = 1; for (int i = 2; i <= n; i++) { fib[i] = fib[i - 1] + fib[i - 1 - [1]]; } foreach (var i in Enumerable.Range(0, n + 1)) { if (fib[i] [2] 0) { fibDict[[3]] = fib[i]; } }
Subtract 1 to get the previous Fibonacci number in the sum. Filter Fibonacci numbers greater than 0. Use i as the key in the dictionary.
