I'm having trouble conceptually understanding the solution to the well-known n-stair climbing problem. The n-stair problem is:
You have n-steps to climb. You can only climb 1 or 2 steps at a time. find the number of ways to reach Nth step.
To keep it simple, let's just use the case where n = 2. The solution is T(n) = T(n-1) + T(n-2) and this is of course the Fibonacci sequence.
The explanation as to why usually goes something like this:
You are at the nth step. How did you get there given that you could climb 1 step or 2 at a time? Well, your previous step must be at either step
n-1(took 1 step) or stepn-2(took 2 steps). Now, there areT(n-1)ways to reach the n-1th step, andT(n-2)ways to reach n-2th steps, which means that there areT(n-2)ways to reachnif your last step was atn-2, andT(n-1)ways to reachnif your last step was atn-1. Those are the only two possibilities of how you finally reached n, so the total number of ways to get to nth step isT(n-1) + T(n-2)
I have trouble conceptualizing the part below:
there are
T(n-1)ways to reach the n-1th step, andT(n-2)ways to reach n-2th steps, which means that there areT(n-2)ways to reachnif your last step was atn-2, andT(n-1)ways to reachnif your last step was atn-1.
This sounds wrong. The explanation seems to contradict itself.
there are
T(n-1)ways to reach the n-1th step
and
and
T(n-1)ways to reachnif your last step was atn-1
And similarly for T(n-2)
I also get confused on a second point. When we say that the solution is T(n-1) + T(n-2) my brain shouts out 'but wait a minute, you're double-counting. T(n-1) already includes T(n-2)'.
Can someone help me conceptually understand the reason why T(n) = T(n-1) + T(n-2)
PS This is not question regarding implementing a solution, but rather a question as to how to explain/understand the answer.