1
00:00:00,110 --> 00:00:04,790
Let's now write the recursive solution for the minimum cost climbing stairs.

2
00:00:04,790 --> 00:00:05,480
Question.

3
00:00:05,510 --> 00:00:08,210
Okay, so we are given a cost array.

4
00:00:08,270 --> 00:00:13,580
Now over here let me say let n be the length of the cost array.

5
00:00:15,410 --> 00:00:15,830
Okay.

6
00:00:15,830 --> 00:00:18,170
And again this is going to be a recursive solution.

7
00:00:18,170 --> 00:00:20,420
So we will need a helper function over here.

8
00:00:20,420 --> 00:00:22,520
And let me just call it helper itself.

9
00:00:22,520 --> 00:00:25,730
And to this function we will be passing an index.

10
00:00:25,760 --> 00:00:28,130
Now let's write the details of this function later.

11
00:00:28,130 --> 00:00:35,870
But ultimately this function is going to return the minimum cost of climbing 1 or 2 steps from this

12
00:00:35,870 --> 00:00:37,340
particular index over here.

13
00:00:37,370 --> 00:00:37,730
Okay.

14
00:00:37,730 --> 00:00:43,910
And also remember the question says that you can either start from the first or the second step.

15
00:00:43,910 --> 00:00:48,650
That is you can start at the step with index zero or with index one.

16
00:00:48,650 --> 00:00:57,170
Therefore, we will ultimately be returning the minimum between helper zero and helper one okay.

17
00:00:58,220 --> 00:01:04,970
Helper zero is the case where you're starting at the step with index zero, and helper one is the case

18
00:01:04,970 --> 00:01:07,730
where you are starting and the step with index one.

19
00:01:07,730 --> 00:01:08,150
All right.

20
00:01:08,150 --> 00:01:11,300
So this is the skeleton of the approach that we're going to take over here.

21
00:01:11,330 --> 00:01:15,080
Now let's go ahead and write the details of this helper function over here.

22
00:01:15,440 --> 00:01:23,360
Now the base case is if the index is greater than or equal to n, it would mean that we have already

23
00:01:23,360 --> 00:01:25,970
reached the top or where we wanted to reach.

24
00:01:25,970 --> 00:01:28,730
And because of that, we can just return zero.

25
00:01:28,730 --> 00:01:33,200
Now, if that is not the case, we will have to calculate two scenarios.

26
00:01:33,200 --> 00:01:40,670
The first scenario is the scenario of taking one step, and the second scenario is the scenario of taking

27
00:01:40,700 --> 00:01:41,390
two steps.

28
00:01:41,390 --> 00:01:47,930
So again in both these scenarios we are using the step at this particular index.

29
00:01:47,930 --> 00:01:51,140
And therefore we will have to pay cost at index.

30
00:01:52,580 --> 00:01:52,940
Okay.

31
00:01:52,940 --> 00:01:55,730
So that is applicable for both of these cases.

32
00:01:55,730 --> 00:02:02,990
Now over here we will have to add to this helper at index plus one because that's where we would reach

33
00:02:02,990 --> 00:02:07,070
if we take one step from this particular step over here okay.

34
00:02:07,070 --> 00:02:10,820
And in this case over here we would be taking two steps.

35
00:02:10,820 --> 00:02:15,200
So therefore we will be reaching index plus two okay.

36
00:02:15,200 --> 00:02:19,940
And then finally we will have to just return the minimum between 1 and 2.

37
00:02:19,970 --> 00:02:24,080
So I can say return math dot min one comma two okay.

38
00:02:24,080 --> 00:02:28,070
Because we want to find the minimum cost of climbing these stairs.

39
00:02:28,070 --> 00:02:28,940
So that's it.

40
00:02:28,940 --> 00:02:30,830
So this should help us solve this question.

41
00:02:30,830 --> 00:02:35,180
Now let's go ahead and run this code and see if it's passing all the test cases.

42
00:02:36,410 --> 00:02:39,530
And you can see that it's passing all the test cases.
