Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to Solve the Staircase Problem with JavaScript using Memoization

function stairSteps(N) {

  // store repeat values in memo to prevent repeat computations
  const memo = [];

  function stepsM(N) {

    if (N === 0) return 1;
    else if (N < 0) return 0;

    if (memo[N] !== undefined) return memo[N];
    else {
      memo[N] = stepsM(N - 1) + stepsM(N - 2) + stepsM(N - 3);
      return memo[N];
    }
  }

  return stepsM(N);
}
Source by indepthjavascript.dev #
 
PREVIOUS NEXT
Tagged: #How #Solve #Staircase #Problem #JavaScript #Memoization
ADD COMMENT
Topic
Name
1+3 =