Climbing Stairs | LeetCode 70

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard


Here is the full implementation.

/**
 * @param {number} n
 * @return {number}
 */
let climbStairs = function(n) {
    let countingFunc = function(stairsRemaining, savedResults) {
      if (stairsRemaining < 0) {
        return 0;
      }

      if (stairsRemaining === 0) {
        return 1;
      }

      if (savedResults[stairsRemaining]) {
        return savedResults[stairsRemaining];
      }

      savedResults[stairsRemaining] = countingFunc(stairsRemaining - 1, savedResults) + countingFunc(stairsRemaining - 2, savedResults);

      return savedResults[stairsRemaining];
    };

    return countingFunc(n, {});
};