const bottomUpFib = function(n) {
if (n < 2) return n
const cache = [0,1]
for (let i=2; i<= n; i++) {
cache[i] = cache[i-1] + cache[i-2]
console.log(`Iteration ${i} ---> \n
Cache ${cache}\n`)
}
return cache[n]
}
console.log(bottomUpFib(5))
//If you go to a more complicated problem, you might need to build a 2d array or matrix and you need to find the solution. Each index in the array, corresponds to a solution to a sub-fib problem. You are creating a lookup table. This is what makes dynamic programming so cool
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter