In this solution walkthrough of the Step Walking challenge, we review: 1. how to use Recursion with the Fibonacci Sequence to solve this in a brute force approach 2. how to optimize our solution using memoization 3. time and space complexity analysis Link to challenge
For 1,2, & 3 steps at a time.
const memo={};const stepMemo=(n,memo)=>{ //1,2, & 3 steps at a time if(memo[n]) return memo[n]; if(n===1) return 1; if(n===2) return 2; if(n===3) return 4; memo[n]=stepMemo(n-1,memo)+stepMemo(n-2,memo); return memo[n]+1;}
console.log(stepMemo(5,memo));
const steps=(nums)=>{ let last=0; let lastLast=1; for(let i=0;i<nums;i++){ [last,lastLast]=[lastLast,last+lastLast]; } return lastLast;}
console.log(steps(5));