Let's now write the recursive solution for the minimum cost climbing stairs.
Question.
Okay, so we are given a cost array.
Now over here let me say let n be the length of the cost array.
Okay.
And again this is going to be a recursive solution.
So we will need a helper function over here.
And let me just call it helper itself.
And to this function we will be passing an index.
Now let's write the details of this function later.
But ultimately this function is going to return the minimum cost of climbing 1 or 2 steps from this
particular index over here.
Okay.
And also remember the question says that you can either start from the first or the second step.
That is you can start at the step with index zero or with index one.
Therefore, we will ultimately be returning the minimum between helper zero and helper one okay.
Helper zero is the case where you're starting at the step with index zero, and helper one is the case
where you are starting and the step with index one.
All right.
So this is the skeleton of the approach that we're going to take over here.
Now let's go ahead and write the details of this helper function over here.
Now the base case is if the index is greater than or equal to n, it would mean that we have already
reached the top or where we wanted to reach.
And because of that, we can just return zero.
Now, if that is not the case, we will have to calculate two scenarios.
The first scenario is the scenario of taking one step, and the second scenario is the scenario of taking
two steps.
So again in both these scenarios we are using the step at this particular index.
And therefore we will have to pay cost at index.
Okay.
So that is applicable for both of these cases.
Now over here we will have to add to this helper at index plus one because that's where we would reach
if we take one step from this particular step over here okay.
And in this case over here we would be taking two steps.
So therefore we will be reaching index plus two okay.
And then finally we will have to just return the minimum between 1 and 2.
So I can say return math dot min one comma two okay.
Because we want to find the minimum cost of climbing these stairs.
So that's it.
So this should help us solve this question.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.