 What's up guys, my name is Michael and welcome to my youtube channel today. We're doing another lee code challenge called matrix cells in distance order I'm gonna try to do a lee code every single day whether it's probably just easy I'll try to finish up easy and then I'll do one every single day and then if worst case scenario once a day if not a Once a week, but I'll try to do once once a day and then if I could get make more if I could do more than once Today, they'll be good if I can't then whatever but yeah, we're gonna do matrix cells in distance order I already did this question, so I'm gonna actually Delete this and then I'll show you guys how I did it So you're given a matrix with row L rows and C columns and it has cells with integer coordinates RC Whereas zero is less than equal to R which is less than the R and zero is less than equal to C Which is less than C. This is just saying that You have a matrix of R and C columns R rows and C columns and then each cell is just like 0 0 0 1 0 1 0 1 1 up to RC. That's what it means. Okay. That's what this means You want to return the coordinates of all cells in the matrix sorted by their distance from R 0 C 0 Okay, so you want to sort it by smallest distance larger distance and the distance between them is just the main hat and distance So you're gonna subtract R1 from R2 plus C1 minus C2 and You use the absolute value of these two. So yeah, that's what we're gonna do I'm gonna assume that you guys already tried this. So I'm gonna do this right now with you guys So we're trying to sort these by Manhattan distance. So first I'm gonna create a vector Right here, I'm gonna create a vector whoops vector vector int and Then this is going to be the to return. This is what we're gonna return What am I gonna do first? I'm going to go through every single row in every single column Okay, this is gonna go to every row and column and what I'm gonna do is I'm actually going to push back the current row and column and Then I'm going to Calculate the distance. So how do I calculate the distance? I'm gonna do distance is equal to R0 minus I plus R0 C0 minus J Okay, so the I and J just represents the current row and column. Sorry, there's an ambulance But that's what this is for and I'm gonna do the I'm gonna do the Absolute value so ABS Okay, so this is basically just getting the distance of R0 and R1 and C0 and C1 Based on this. This is just gonna go I and J just goes through every single row and column. Okay, so what I'm gonna do is I'm gonna crew basically add each row and column and then the distance it took Into my to return which is my vector vectors my matrix so it's going What this this notation idea this notation of having brackets that's just an easier way to create a vector that has these values so basically, I'm just going to add a new array that has The row the column and then this the distance. Okay, that's what I'm doing Now what I'm going to do is I'm going to sort I'm going to sort the Array by beginning And then and then I'm going to do this new notation This is basically I'm gonna pass in a comparator that is a function that lets you do that In this comparator, it's going to take in two values It's gonna take in first and then a vector containing second. So that's just going to represent the the two two arrays I'm going to compare to okay So how am I gonna sort this I'm gonna sort it by this comparator function and this comparator function It's going to take the the smallest distance so Given my first two arrays. I gotta make this pass by reference I'm going to return the smallest distance which in this case it would be first at the second and second at second The reason why it's two is because it's the second value of our Array whenever we added values right whenever we added this each row We add the row column in the distance So I'm sorting it by the distance and distance is at the index zero one two That's what this is for so this is going to return the array that has that's what this value is it's going to return the array based on the having the The distance compared the smallest distance was going to compare that and that's going to sort this by smallest distance After that we're going to go through Every we're going to go through every of our vector again This is Current and go through every single Vector in our to return and what we're going to do is we are going to pop back the last value reason why we're going to do that is because we want to get rid of all the last distance so for every For every vector inside our Matrix of to return we're going to try to get rid of this distance now Pop back is going to remove the distance from it because once we sorted it by distance We don't need it anymore. We just well. That's why we want to remove it So yeah, I'm gonna use pop up and remove this distance Okay, let's run the code and It got the expected value. Let's submit it and it got accepted So yeah, that's that's a way how you would do it That's one way you could do this question another way you could do it is using bucket sort another way You could do it using breadth first search. I think this is much easier because You're actually calling the sorting function instead of actually creating your own sorting But yeah, that's how you do this question rate comp subscribe. I'll check you guys later I'm gonna do another question and then yeah