Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 1.28 KB

dynamic-programming.md

File metadata and controls

34 lines (28 loc) · 1.28 KB

Dynamic Programming

function fib(n) {
    if (n <= 2) {return 1;}
    return fib(n-1) + fib(n-2);
}
//or (my one liner)
//const fib = (n, memo={2:1, 1:1}) => memo[n] || (memo[n] = fib(n-1, memo) + fib(n-2, memo))
const fib = (n, m={2:1, 1:1}) => m[n] || (m[n] = fib(n-1, m) + fib(n-2, m))

// Try time complexity of fib(50)

console.assert(fib(6) == 8)
console.assert(fib(7) == 13)
console.assert(fib(8) == 21)