|
| 1 | +# [Recursion: Davis' Staircase](https://www.hackerrank.com/challenges/ctci-recursive-staircase) |
| 2 | + |
| 3 | +Find the number of ways to get from the bottom of a staircase |
| 4 | +to the top if you can jump 1, 2, or 3 stairs at a time. |
| 5 | + |
| 6 | +- Difficulty: `#medium` |
| 7 | +- Category: `#ProblemSolvingIntermediate` |
| 8 | + |
| 9 | +Davis has a number of staircases in his house and he likes to |
| 10 | +climb each staircase `1`, `2`, or `3` steps at a time. |
| 11 | +Being a very precocious child, he wonders how many ways there |
| 12 | +are to reach the top of the staircase. |
| 13 | + |
| 14 | +Given the respective heights for each of the staircases in his house, |
| 15 | +find and print the number of ways he can climb each staircase, |
| 16 | +module $10^10 + 7 $ on a new line. |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +`n = 5` |
| 21 | + |
| 22 | +The staircase has `5` steps. Davis can step on the following sequences of steps: |
| 23 | + |
| 24 | +```text |
| 25 | +1 1 1 1 1 |
| 26 | +1 1 1 2 |
| 27 | +1 1 2 1 |
| 28 | +1 2 1 1 |
| 29 | +2 1 1 1 |
| 30 | +1 2 2 |
| 31 | +2 2 1 |
| 32 | +2 1 2 |
| 33 | +1 1 3 |
| 34 | +1 3 1 |
| 35 | +3 1 1 |
| 36 | +2 3 |
| 37 | +3 2 |
| 38 | +``` |
| 39 | + |
| 40 | +There are `13` possible ways he can take these `5` steps and `13 modulo 10000000007` |
| 41 | + |
| 42 | +## Function Description |
| 43 | + |
| 44 | +Complete the stepPerms function using recursion in the editor below. |
| 45 | + |
| 46 | +stepPerms has the following parameter(s): |
| 47 | + |
| 48 | +- int n: the number of stairs in the staircase |
| 49 | + |
| 50 | +## Returns |
| 51 | + |
| 52 | +int: the number of ways Davis can climb the staircase, modulo 10000000007 |
| 53 | + |
| 54 | +## Input Format |
| 55 | + |
| 56 | +The first line contains a single integer, `s`, the number of staircases in his house. |
| 57 | +Each of the following `s` lines contains a single integer, |
| 58 | +`n`, the height of staircase `i`. |
| 59 | + |
| 60 | +## Constraints |
| 61 | + |
| 62 | +- $ 1 \leq s \leq 5 $ |
| 63 | +- $ 1 \leq n \leq 36 $ |
| 64 | + |
| 65 | +## Subtasks |
| 66 | + |
| 67 | +- 1 \leq n \leq 20 for `50%` of the maximum score. |
| 68 | + |
| 69 | +## Sample Input |
| 70 | + |
| 71 | +```text |
| 72 | +STDIN Function |
| 73 | +----- -------- |
| 74 | +3 s = 3 (number of staircases) |
| 75 | +1 first staircase n = 1 |
| 76 | +3 second n = 3 |
| 77 | +7 third n = 7 |
| 78 | +``` |
| 79 | + |
| 80 | +## Sample Output |
| 81 | + |
| 82 | +```text |
| 83 | +1 |
| 84 | +4 |
| 85 | +44 |
| 86 | +``` |
| 87 | + |
| 88 | +## Explanation |
| 89 | + |
| 90 | +Let's calculate the number of ways of climbing |
| 91 | +the first two of the Davis' `s = 3` staircases: |
| 92 | + |
| 93 | +1. The first staircase only has `n = 1` step, |
| 94 | + so there is only one way for him to |
| 95 | + climb it (i.e., by jumping `1` step). Thus, we print `1` on a new line. |
| 96 | + |
| 97 | +2. The second staircase has `n = 3` steps and he can climb it in any of the |
| 98 | + four following ways: |
| 99 | + |
| 100 | + 1. 1 -> 1 -> 1 |
| 101 | + 2. 1 -> 2 |
| 102 | + 3. 2 -> 1 |
| 103 | + 4. 3 |
| 104 | + |
| 105 | +Thus, we print `4` on a new line. |
0 commit comments