Skip to content

Instantly share code, notes, and snippets.

@jasonwaters
Created February 9, 2017 18:28
Show Gist options
  • Save jasonwaters/a9cd8120a8fe37c7a3583ff0b36e1a69 to your computer and use it in GitHub Desktop.
Save jasonwaters/a9cd8120a8fe37c7a3583ff0b36e1a69 to your computer and use it in GitHub Desktop.
Recursion: Davis' Staircase
//https://www.hackerrank.com/challenges/ctci-recursive-staircase
function stairStepper(stepSizes) {
let answers = [1,1];
return function(numSteps) {
if(numSteps < 0) {
return 0;
}else if(typeof answers[numSteps] === 'undefined') {
answers[numSteps] = stepSizes.reduce((totalSteps, stepSize) => totalSteps + stairCombinations(numSteps-stepSize), 0);
}
return answers[numSteps];
};
}
const stairCombinations = stairStepper([1,2,3]);
[1,3,7, 10, 40].forEach(value => console.log(stairCombinations(value)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment