Complete the code to declare the memo object for caching results.
const memo: [1] = {};We use a Record with number keys and number values to store computed results for each input number.
Complete the code to check if the result for n is already cached in memo.
if ([1] in memo) return memo[n];
We check if the input number n is a key in the memo object to reuse the cached result.
Fix the error in storing the computed Fibonacci value in memo.
memo[n] = [1] + [2];
We store the sum of previously computed Fibonacci values from memo to avoid repeated recursion.
Fill both blanks to complete the memoized Fibonacci function.
function fib(n: number): number {
if (n <= 1) return n;
if (n [1] memo) return memo[n];
memo[n] = fib(n - 1) [2] fib(n - 2);
return memo[n];
}We check if n is in memo and then add the results of fib(n-1) and fib(n-2) to store in memo.
Fill all three blanks to create a memoized factorial function.
const factorialMemo: Record<number, number> = {};
function factorial(n: number): number {
if (n <= 1) return 1;
if (n [1] factorialMemo) return factorialMemo[n];
factorialMemo[n] = n [2] factorial(n [3] 1);
return factorialMemo[n];
}We check if n is in factorialMemo, then multiply n by factorial(n-1) and store the result.