Complete the code to initialize the base case for Fibonacci DP array.
int[] fib = new int[n + 1]; fib[0] = [1];
The Fibonacci sequence starts with 0 at index 0, so fib[0] should be 0.
Complete the code to initialize the second base case for Fibonacci DP array.
fib[1] = [1];
The second Fibonacci number fib[1] is 1.
Fix the error in the loop to correctly compute Fibonacci numbers using DP.
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 static int fibonacci(int n) {
int[] fib = new int[n + 1];
fib[0] = [1];
fib[1] = [2];
for (int i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}The base cases for Fibonacci are fib[0] = 0 and fib[1] = 1.
Fill all three blanks to create a map comprehension that stores Fibonacci numbers for keys 0 to n where value is fib[key].
Map<Integer, Integer> fibMap = new HashMap<>(); for (int i = 0; i <= n; i++) { fibMap.put([1], [2]); } return fibMap.get([3]);
We put key i and value fib[i] in the map, then return fibMap.get(n).
