 What's up guys Mike the coder here today? We are going to go over another problem on spodge Sorry guys, if I haven't actually been uploading in a long time because I don't know I Just had I had too much work to you last week and the week before but yeah today We're gonna go over another spodge question This statement is the story is too long. So this is called philosopher's stone by tismet to Okay, basically you just have a bunch of stones 100 and then this guy who's I don't even know his name is but Harry Yeah, Harry you can only go to the next row So you could go to the next row and you collect stones from that tile and so on and so forth until you go to the last row so what he wants to do is like he's basically like He has this like grid of stones here And then he could go to the next row and you he just wants to collect all the stones on the current row or collect the stones from the rows until the last row right and You can move one tile to the next row. So you can only move either Below it or diagonal to the left or right. So basically you're given the values of h and w and You have all the values of the stones and you want to calculate the maximum possible number of stones Here you can grab from one trip from the first row to last so from here to the last, okay? All right, so basically to do this problem. You could build up You basically could just sum up all the values. It's kind of like prefix some but then what you're gonna do is you're gonna go through to the The robe right below it right and then the road to the left diagonal to the left and the row diagonal to right so we could actually just bring this up by going to print screen and I could show you what I mean by that So let's go back here All right, so let me just real quick Just cut this out Yeah, right here this I learned this is much faster Way faster. Okay, so now I'm just gonna do an edit. Oh, no That must be a way to crop Whatever it doesn't matter Yeah, we could just use this So let's let's let's take this take a look at this Yeah Okay. Yeah, so let's take a look at this and Let me zoom out Okay, so let's let's just actually number the rows and columns with like zero to one So you have zero one two three four five And here zero one two three four, so let's say this is row Rows and this is columns, right? That's how it is. Okay, so remember let's say we start from the first row and Basically here you want to collect the maximum number of rows starting from this row to the last Right for scissors the first row to the last row But you can only go to the diagonal to the left down and to the right So on this side from three you can't really go diagonal to the left because there's nothing here, right? You're on the right side, so you only could go you only could go down one So you could pick here and the other choice you could pick is right here, right? So you could only pick Three you could pick two or one, right the options you could do, right? So what you could do is that you could actually like sum up the values from what you're currently going to and at that point if you keep going all the way to the bottom you're gonna have the maximum possible something could have in it So how how are we gonna sum up the values? So first of all, let's look at the Let's look at the indexes, right? So if I'm gonna go diagonal to the right So what is the index of diagonal to right? So from here from three diagonals to the right? There's a it's an index one row one column one, right? So that's one so from here to the right diagonal. It's row one Column one, right? I'm gonna write it this way and then From the first part here my original index. I'm starting at so my origin is Actually zero zero Right zero zero. So what I could do is I could basically in order to go diagonal to the right What I could do is I could just add one to the row in the column, right? So then at this point It's I chose it from one one. I want to go diagonal to the right. I'll be two two So that would be here too, right? So now for that part, I would just add one to the row and column So what I'm gonna do is I'm just I'm just gonna like automatically sum this up. So I have three one I'm gonna sum this up. So let's be three three plus one would now be four So this value is gonna be four, right? And then then here I could go either down or to the right Right, so then I do four plus two with six Right, and then four plus two is six, right? So I'm just gonna keep doing that Right and while I'm going doing this I'm gonna actually get the maximum sum that I could possibly do and I set those values So like here we have zero two, right? So that's two zero. No, that's three three Three plus two would be five Right, so that'll be five and then five plus two would be six, right? And then Here we have the difference the sum four four and four and six see which was the largest one you picked the maximum and we're just gonna continue doing That replacing all the values until we have all the values in last row So then at the end we're gonna have like a sum of all the maximum values of going down to the diagonal to the left to the right And then in the end we're just gonna pick the maximum from the last row. So yeah going to the right We're just gonna add one to each of the indexes from the current row to the next one to the right And then if we want to go left, let's look at how do you go left? So Yeah, to go left, let's say I was at four right because we have to go through all the all the different possible ways to go Right, we left down right. So let's say I'm at four then to go diagonal to the left. That's at one two right row one column two That'd be this three and what is it from zero three, right? So our origin was zero three now It's one two. So if I want to go left what I'm gonna do is I'm going to actually add one to the row Right add one to the row. So row will get like plus one. So those would be like two plus one to the row and then Then subtract one from the column, right? So like the current column. So for origin of here would be zero three, right? This is origin I'm at zero three for the number four. This is a number four So at number four here, right? It's at row zero column three So if I want to go diagonal to the left, there'll be row one column two So that's row one column two and for that I'll just add one to the X value and subtract one for the Y value So that's going diagonal to the left and then if you know if you just want to go directly below it You just add one to the row, right? And then at that point We're just gonna loop through from the all the values in the last row and just get all the maximum because at that point When we go through every single row do this for all the values from like the first row for starting from zero zero Then starting for zero one zero two zero three zero four and so on so forth until you get to the rows of five The last value the last row will have all the maximum possible values that you have and then at that point You just look through the last row and then you pick the largest one. So yeah, that's basically just how to do it I'm gonna show you my code for it Okay, so let's let's go over it now Yeah, so here we have Read the number of test cases t read in nm This is the number of rows number of columns and the read in our matrix d of ij So what I'm gonna do is I create a DP array. This is called DP Actually, no, I never I never actually use DP my bad I never actually use DP because uh, we just we're just summing up the values So we don't actually need a DP array, but yeah, then what I'm gonna do is um I'm gonna start from the first row and go to the last column and the reason why I start from the first row It's gonna I'm gonna start here, right and at this at this row This is the only row where you when you could start going down as a go down one or to the right Right, this is the first starting from the first row to the end is actually makes more sense Because these are the values you have to set to so yeah I start from the first row and go to the bottom and then here I'm gonna start from the j is zero to m which is gonna go through the columns So this is gonna go to the right, right? This is gonna go all the way all the values to the right So hold up. Let me just clear everything real quick Yeah, we just clear everything No, I can't clear Actually, I could just clear Hold up. Sorry guys Okay, yeah Clear everything real quick Real quick. All right so Back at this again. So now let's look at this So basically I'm starting from row one So this is row one and I'm looping down this way and then I'm gonna go through all the columns to the right So that's what this is doing High is just gonna represent like the smallest possible value could have for the current value So what I'm gonna do is I'm gonna go to oh, yeah If I'm at the far the far the left side, so like j is equal to zero, right? J is equal to zero. So this is like the far this left side right here Right, then I only could go down and right I can only go and die either one row below it or diagonal one to the Right, so I can't actually go diagonal to the left because like yeah, I mean you're at the border You can't really do anything right if you go you did it doesn't exist when you do that So for that case if j is equal to zero, right? We're just gonna go down one We're gonna find the maximum value between the row right below it and the row diagonal to it to the right So that's what this is d of i minus one and j and d of i minus one j plus one So that's the maximum value between here and here Okay, that's what this is right here and here Okay All right, um Yeah, I Think that's what yeah, yeah, and then um here of j is equal to the m minus one And minus one is also the same last value. So that's like right here Right if j is m minus one, so that's like we are on the border of here so in this case you only could go to diagonal to the left and Below it right you only could go these here because you can't go to the right So in that case we try to find the maximum value for the one diagonal to the left and the one Right below it. So that's why that's what this is for right, so This value would just get like the highest value the maximum value for those parts. All right, otherwise If you're not in like any of these two situations where you're on the borders Then you're gonna actually have to find the maximum between all three different types, which is in this case, it'll be like It'll be like let's say I'm over here in the middle Right, I'm looking through to the middle of the nap the have a choice of going down here go here and Go here right diagonal to the left diagonal down diagonal to the right So I need to all find all three of these choices and for that I just Yeah, I need to find the maximum value for all three of these cases So that's what I do here. So that's I miss one j plus one. I miss one j I miss one j minus one So that's high and then at that point We're gonna just sum of the current values with that with the largest value for that part. So like This would make a d of ij plus equal to high. So what that means is that this three would get replaced with? Three plus two right because the highest value here is all two so that they'll get replaced three plus two So that'd be five and we're gonna keep going that doing that for all the values until we get to the last last row so once we end their last row we just need to Loot through all the values in the last row. So for that I just start from J is equal to zero and go up to the number of columns, right? So that's that's going this way and then for that I just have like a maximum which is like the largest possible value here And I just check get the maximum value for all the values from n minus one and j So n minus one is the last last row here, right? If we have n rows That's the last row is gonna be n minus one and we're gonna find all the values there That's the maximum. So that's what that's this loop is here and at that point. We just print maximum So, yeah, I hope you guys enjoyed this video where it comes subscribe I'll check you guys later. Sorry guys. I had to like upload edit some stuff in the middle But yeah, we come subscribe about you guys later. Peace