Complete the code to declare the memo map for storing computed results.
var memo = make(map[int]int[1])The memo map must be initialized with curly braces {} to create an empty map in Go.
Complete the code to check if the result for n is already computed in memo.
if val, ok := memo[[1]]; ok { return val }
We check if the key n exists in the memo map to reuse the stored result.
Fix the error in the recursive call to compute Fibonacci with memoization.
result := fib([1] - 1) + fib([1] - 2)
The recursive calls must use the input parameter n to compute smaller subproblems.
Fill both blanks to store the computed result in memo and return it.
memo[[1]] = [2] return memo[[1]]
Store the computed result in memo[n] and return it to reuse later.
Fill all three blanks to complete the memoized Fibonacci function.
func fib(n int) int {
if n <= 1 {
return n
}
if val, ok := memo[[1]]; ok {
return val
}
result := fib(n-1) + fib(n-2)
memo[[2]] = [3]
return memo[n]
}The function checks if n is in memo, returns it if yes, otherwise computes result, stores it in memo[n], and returns it.
