Optimize recursive problems using memoization (top-down) or tabulation (bottom-up). Trade space for time: O(n) space, O(n) time instead of O(2ⁿ) time.
// Memoization: top-down, avoid recomputation
function fib(n, memo = {}) {
if (n in memo) return memo[n];
if (n <= 1) return n;
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
return memo[n];
// ...Optimize recursive problems using memoization (top-down) or tabulation (bottom-up). Trade space for time: O(n) space, O(n) time instead of O(2ⁿ) time.
// Memoization: top-down, avoid recomputation
function fib(n, memo = {}) {
if (n in memo) return memo[n];
if (n <= 1) return n;
memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
return memo[n];
// ...