- 2 weeks ago
Transcript
00:00Next question which we will talk about is Grid Ways.
00:03What is Grid? Grid is like a 2D matrix.
00:06There are some columns and some rows.
00:10What do I have to do in this question?
00:11I have to tell the number of ways
00:13which I have to tell this cell number.
00:15Which cell number is? This is my 0,0 cell number.
00:21This cell number is n-1,m-1 cell number.
00:25I will go to the other side and my Grid is n x m.
00:29Which cell will be n-1,m-1 cell?
00:32This cell will be n-1 is row and m-1 is my column.
00:37What do I have to do?
00:38I have to tell the number of ways
00:40which I will tell the target point
00:44or to the target cell.
00:49Or to the source, I will tell the destination.
00:52I can go to the source or where we start.
00:55The destination is where we reach.
00:57I will tell the total number of ways
00:59but a small twist is that I have two allowed moves.
01:03What are the allowed moves?
01:05Either I can go to the right side or down.
01:09I can't go to the left side.
01:11What is the style this parallel way to our boss?
01:12If you want to mess around,
01:15because I can put a wrong sign here,
01:17to the right side,
01:18then I should go down,
01:18then around 3 times,
01:19then down two times.
01:20So I can't go up in the right path.
01:22Which we can do the same path I can do.
01:23Where I move in right,
01:24then I move in left,
01:26then I move in right and that more control.
01:28That's not allowed.
01:30Hence, that's allowed moves.
01:31The two allowed moves have only uhh.
01:32two are, one right and one down, and tell me the total number of ways, how to move right
01:38and down, I can reach the target cell to my source, now think about solving this approach
01:46let's say this is my target cell, this is my target cell, now target cell, I start
01:52with my source to target cell, if I sit in 0,0, then I can go here in two directions,
01:58I can go from one direction, so I reached here, if I took the right direction, and if I
02:06took the right direction, then I will reach here, this is my down step, as I reached this
02:12cell, what can I say, if I had a problem from this cell, to reach here, if I reached this
02:20cell, then my problem has become here, because I cannot go left, I mean, I cannot go here
02:28left, I can go in any direction, just go right and down, so my problem has reduced, that
02:35I will become a mini grid, that I will reach this cell to this target cell, and if I reached
02:43this down direction, then my problem has reduced, I can only go down and right, I will not go
02:52to the right direction, so my grid will be reduced to this mini grid, in which I will
02:56work to take this source cell to this destination, or this target cell, so I have two choices,
03:04I have two choices, the first choice is my right choice, the second choice is my down choice,
03:10just like I am cell, x, y, if I have moved in this direction, then I will move in the
03:16right direction,
03:17I will move in the right direction, that means y, its value will increase by one, so in the right
03:22direction,
03:23my problem has reduced, for x, y plus one, and if I have gone down, I mean, this direction
03:30has reduced, my problem has reduced, x plus one, rho has increased by one, so my problem has reduced
03:36to x plus one, y, so what can I say, if I have removed all the parts here, and I
03:46have removed all the parts
03:49here, all the ways, if I have removed all the ways, after going right, that is equal to w1, and
03:56down
03:57after going right, that is equal to w2, I can say that w1 means these ways, and w2 means these
04:05ways,
04:06and w2 means these ways, that if I take a sum, what will be the sum, w1 plus w2, my
04:11sum will be
04:12equal to total ways, I know, taking one step, then I will reach here, now, all the steps
04:19are, if I add them, here and here, all the steps, then the sum will be equal to my total
04:26number of ways,
04:27here and here, all the steps, now, let's think about the base case, now, I will write it a little
04:34bit more recursive fashion, recursive fashion means that if my function is recursive function x, y, x is my
04:44row, y is my column, so, if I have a cell number x, y, and here, I will go to
04:51my target cell
04:52to my target cell, what can I say, that this function will be equal to, if I take x plus
04:571, y,
04:59I will take down step, plus I take ways to take x, y plus 1, this is my right step,
05:08if I have
05:09left the cell cell after down, and left the cell after right, so, I will add both ways,
05:14that will be my total number of ways, so, this will be my recurrence relation, or my recursive
05:21equation, I have present my recursion, now, what will be the base case, this can be the base case,
05:38it will be to me necessary to challenge, and the base case I need the base, that means,
05:40I need to target target, so, how many ways? as well.
05:45As soon as I call it, it speaks zero ways, not only results, but 0 ways means possible,
05:49it means possible, immediately vs wrestler like one way means possible, and this
05:51пу 이야기 can move depending on one way is possible, so, I can tell you, go to target
05:54target to target, it is one way, but we don't have one way, there is always one way to
05:59So, target série can be one way to target terms, see nak to go back up, no direction,
06:05If you are already on target or down, you are already on target.
06:07So there is one way.
06:08If I look at this thing recursively,
06:11I had to start here and go here.
06:14So what I can say?
06:15There is one way here.
06:17Now if I talk about this cell,
06:19there are zero ways.
06:21How many ways are down? One ways.
06:23So total will be 1 plus 0 is equal to 1.
06:26If I talk about this cell,
06:28there is one way here.
06:30How many ways are down?
06:31Why is down?
06:32Why is zero ways?
06:33Because once our row is greater than n
06:36It will be zero ways
06:37because if my value is greater than n
06:40What is in this case?
06:41There is no way here.
06:42Once you are going down,
06:43we will not get up here.
06:45So this will be 1 plus 0 is equal to 1.
06:50I will write this in a clean fashion.
06:52So here I have one ways.
06:54Here I have one ways.
06:55If I talk about this cell,
06:57how many ways are here?
06:58So how many ways are here,
07:00that's 1 way here.
07:01How many ways?
07:14How many ways are here?
07:28Down means,
07:28So this cell has to reach the target to reach this cell.
07:34Which way?
07:37I am going right, right, down.
07:39I am going right, down, right.
07:42I am going down, right, right.
07:44How many ways are going here?
07:46Three ways are going.
07:47My approach is that every time my cell has two choices.
07:52One is going right and one is going down.
07:54And with these choices, I have my recursion technique.
07:59Recursion technique says that if I have a waste of x, y, then take x plus 1 and then take
08:07x plus 1.
08:08This will give w1, this will give w2.
08:11When we add both, the total number of waste will be my original cell.
08:16Now we are going to write the code.