Dynamic Programming

Run Settings
LanguageJavaScript
Language Version
Run Command
function memoizedAddTo80(){ let cache = {}; return function(n){ if(n in cache){ return cache[n]; } else { console.log('long time'); cache[n] = n + 80; return cache[n]; } } } const memoized = memoizedAddTo80(); console.log('1',memoized(85)); console.log('2',memoized(86)); let calculations = 0; function fibonancci (n){ calculations++; if(n < 2){ return n; } return fibonancci(n-1) + fibonancci(n-2); } function FibonacciMaster(){ let cache = {}; return function fib(n){ calculations++; if(n in cache){ return cache[n]; } else { if(n < 2){ return n; } else { cache [n] = fib(n-1) + fib (n-2); return cache[n]; } } } } const fasterFib = FibonacciMaster(); console.log('DP',fasterFib(10)); console.log(calculations);
Editor Settings
Theme
Key bindings
Full width
Lines