Article type: Video

Step Walking Solution

mrdaniel published this on 8/22/20

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


Comments (2)

0

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));


Ankur1397 commented on 07/05/21
0
For Constant Space Complexity.
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));
Ankur1397 commented on 07/05/21
Log in to submit a comment.