Created
February 9, 2017 18:28
-
-
Save jasonwaters/a9cd8120a8fe37c7a3583ff0b36e1a69 to your computer and use it in GitHub Desktop.
Recursion: Davis' Staircase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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