"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: You are running up n steps. If you can take a single, double, or triple step, how many possible ways are there to run up to the nth step?\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* If n == 0, what should the result be?\n",
" * Go with 1, but discuss different approaches\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"* None or negative input -> Exception\n",
"* n == 0 -> 1\n",
"* n == 1 -> 1\n",
"* n == 2 -> 2\n",
"* n == 3 -> 4\n",
"* n == 4 -> 7\n",
"* n == 10 -> 274"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"To get to step n, we will need to have gone:\n",
"\n",
"* One step from n-1\n",
"* Two steps from n-2\n",
"* Three steps from n-3\n",
"\n",
"If we go the one step route above, we'll be at n-1 before taking the last step. To get to step n-1, we will need to have gone:\n",
"\n",
"* One step from n-1-1\n",
"* Two steps from n-1-2\n",
"* Three steps from n-1-2\n",
"\n",
"Continue this process until we reach the start.\n",
"\n",
"Base case:\n",
"\n",
"* If n < 0: return 0\n",
"* If n == 0: return 1\n",
"\n",
"Note, if we had chosen n == 0 to return 0 instead, we would need to add additional base cases. Otherwise we'd be adding multiple 0's once we hit the base cases and not get any result > 0.\n",
"\n",
"Recursive case:\n",
"\n",
"We'll memoize the solution to improve performance.\n",
"\n",
"* Use the memo if we've already processed the current step.\n",
"* Update the memo by adding the recursive calls to step(n-1), step(n-2), step(n-3)\n",
"\n",
"Complexity:\n",
"* Time: O(n), if using memoization\n",
"* Space: O(n), where n is the recursion depth\n",
"\n",
"Note: The number of ways will quickly overflow the bounds of an integer."