 What's up guys my is Michael and welcome to my youtube channel today We're gonna do a lego challenge of k closest points in the origin. So this is called find the kth closest points To origin and it's another heat problem So you're given a list of points on a plane now. We got to find the k closest points to the origin so the first point they give us was in the example is One three negative two and two so they give us the point one three Hope you guys could see this. Yeah, you could okay one three negative two and two and That point if we graph it on this graph. It's one One one two. This is one and this is one two three. This is One three is here and then negative two is here. Let's say this negative one negative two Negative two two is here. All right. This is the first point and this is second point, right? And basically they want us to find the k closest points in the org to the origin Based on the Euclidean distance So using the distance formula for this point It would be the distance for the point one three is going to be one square plus three square and square root so in this distance is going to be One plus nine which is square root of one plus nine is square root of ten. Yeah, so this distance squared to ten for this point P one we'll call this P one P one a point one and we'll call this point two and Point twos distance is going to be To negative two square plus two square and then square root and that's four plus four Which is square root of four plus four which is square root of eight So this distance the second point P two is going to be squared of eight so they want us to find the K closest points to the origin. Okay, so what does that mean? That means the whichever is the closest value to the origin That would be the The shortest distance to the origin so in this case if if K is equal to one That means I want to find the shortest point to the origin that Which is the first closest point to earth to origin that it's going to be based on this distance would be square root of eight, which is P2 and Because of that the reason why it's Square root of eight is because this distance is the smallest out of The first two points right because square root of eight is less than square root of ten The they want they want us to return P2 so that would be negative two two So if K equals to the one the smallest distance towards the origin is going to be Square root of eight and they want us to return that point which is negative two two Okay Okay, so that's only for K equals a one Which is the first closest point to the origin now? What if it's the second closest point? So in the second example, we're gonna we have another example here And I'm gonna flip the page over In the second example we have the values of three three Five negative one negative two four Okay, so these are the points we have and they want us to find K equal to which is the to close to close this point To the origin That's what it means. Okay, so if I were to draw this value the values on in a graph three three We'll call this P1 P2 P2 P3 So P1 is three three So P1's distance is gonna be square root of three square plus three square and that's gonna be Squared of 18, so this distance is gonna be Squared of 18 Let's see P2 is gonna be five negative one, send it five negative one And its distance is gonna be five square plus negative one square, and that's going to be 25 plus one Which is squared of twenty-six, so this this distance squared to 26 is going to be P2 P3's distance is going to be negative 2 square plus 4 square, square root of that, that's going to be 4 plus 16, which is square root of 20. And that's going to be this distance, negative 2, 4, so negative 2, 4, and it's going to be P3. And this is going to have a distance of square root of 20. So we want to find the two closest points to the origin. And in this case, it's going to be the two smallest values with the smallest distance because that is the two closest points to the origin, right? It's the two smallest values, okay? So in this case, it would be square root of 18 and square root of 20. And what is square root of, so the two, if K equals the two, two closest points to origin is going to be P1 and P3, P1 and P3, because they have the smallest square root, right? P1 and P3. The reason why is because their distances are the smallest, smallest compared to the other ones, compared to P2, yeah. So the two closest points to origin is P1 and P3, mainly because of their distance. Like if I put the distances, the distance of P1, let me square root of 18, distance of P2, I mean my P2, P3 is square root of 20. Out of all these distances, square root of 18, square root of 20 is the two closest points to the origin because they have the smallest distance towards the origin. So this problem could be broken down into, sorry guys, can you see it? Yeah. This problem basically comes down to the same problem that we saw in the first two videos. It's basically the same thing as finding the kth smallest value in an array, mainly. Because the only difference that they changed here is that the, they added a different another point, like a two coordinates, that's the only thing they added. And the only thing they added was rather than doing, rather than actually finding the kth smallest points, kth smallest elements in an array, they're doing it based on distance, right? So this is basically the same problem as finding the kth smallest element in an array. So how do we do this? For one thing, as I showed you before, we could calculate every distance between every point to the origin using the distance formula. Then we could sort it, put the values into an array, sort it. And then we loop through from the beginning towards k. And that would give us the, the, the values that are the kth closest to the origin. Like if I were to have all these distances. So let's say I put these values into an array, squared 18, squared to 26, squared to 20. Okay. This is going to be P1, P2, P3, then I sort it, sort the values, this is after sorting. I'm going to get squared to 18, squared to 20, square root of 26. So the corresponding value, if I want to find the two closest points to the origin, it was to be the smallest values in our array after sorting. And that would be, whoops, yeah, hope you guys can see it. I can't see it from here, but yeah, that would be squared of 18 and squared of 20. So if we put all the values into an array and then sort it, we would get an iterate from the index zero, one, two, we would get the right values. Okay. Now what is the issue with this? The problem is that the space, if we were have to cut with that, if we were going to do it this way, we would have to basically add all the values of size n into an array, iterate it, iterate from the beginning towards up to K, K2, which is like a K up to K, which is two, go up to two, then we could get the answer like that. But the problem with that is that adding the values to the array, then sorting it is going to take n log n and then the space complexity would be size n. We want to lower that. We don't want to use all of the value, all the points inside the space. So we could change the complexity down towards size K using a heap. So just as using a heap, where you're going to do it this way. So if you were to use a heap, let's say because they want the smallest values, the case smallest values, based on the distance towards origin, if I were to use a min heap, right, that put all the small values in the beginning, I could use a min heap. And then that basically I could do the exactly the same thing as sorting here, basically what it's doing, you're going to keep adding the values into the min heap up to size n of n points, because there's n and there's n points right up to size n, keep adding the values up to the size n and use a min heap. Then we could keep after we add all the values into the min heap, it's going to be sorted in increasing order. So the smallest values on the beginning. And if you were to keep removing the first value, you would get the if you keep moving the first value up to size of K, you would get the K closest or points to the origin. Problem with this is exactly the same thing as the previous issue of maintaining size of n in our heap. So that's an issue. So what we're going to do is we're actually going to use a max heap. We're going to create a max heap of size K equal to 2 of K equal to 2. And then we'll do the exactly the same thing as what we did in the previous videos of instead of finding the kth largest value of our array, we're going to find the kth smallest value of array. So if I were to maintain a heap, I'm going to get a separate sheet of paper. If I were to calculate all the values using the distance formula from the origin and then maintain a size of K equal to 2, well, here's what I'm going to do. So based on our previous points, we have square root of 18 squared of 26 squared of 20. And these are the distances in our array. We want the kth smallest values in our array. So what I'm going to do, let's say K equals the two, I'm going to create a size of max heap of size two of size of K, which is in this case is two that maintains the values max heap. Okay, I'm going to iterate from the array, and I'm going to add the values into my max heap. So in this case, I'm going to add square root of 18, and square root of 26. Max heap is going to sort these. So after this iteration, max heap, what it does is it's going to put those largest values first, then the smallest value. So we're going to have square root of 26 and square root of 18. After it does this, right? Once my max heap is filled up to K equal to I'm going to keep, I'm going to keep iterating through my array. So in this case, I'm going to go to the next value square root of 20 is 20 is square root of 20 less than the first value of my max heap. Yes, it is. So what am I going to do? I'm going to remove the first value of my max heap, and then add square root of 20. So in this case, it's going to add square root of 20, like this. But then because of it's a max heap, it's going to reorder these values using heap of five. So it's going to like swap. So this is actually going to change into square root of 20, and then square root of 18. Right. The next array square root of 20 and square root of 18. Let me see. I'm going to get a racer. This seems hard to read. So once I add square root of 20, square root of 18 is going to be square root of 20 and square root of 18 because it's a max heap. Then after I'm done with the array pretty much at the end of the my iteration of my array, I'm going to have a size of all the values of the case closest points to the origin, square root of 20 and square root of 18. And that's exactly the same thing as what we had in the previous example of square root of 18 and square root of 20. So now the problem is that we need to also maintain the, we also have to maintain, we have to get the points, the specific points. And to do that, what we could do is that in our implementation, we could pair up every distance with the index of the point each point in the array. Alright guys. So now we're going to do how to code this up. Basically, case closest points to the origin first. We got to calculate the distance towards the origin first. So what am I going to do first? I got to move the microphone for a second. Create a double. It's going to return a double and I'm going to call this distance. This is going to be the distance towards origin for giving a point. So I'm going to call this point point. Remember, based on the example, a point just is just an array that has two values, the x corner and the y coordinate. So to do this, I'm just going to do return square root of point at zero. Remember, a square plus b square in the square root of that. So a square, that's point at zero plus b square, which is the same. We're just point at one and then square with that. That's what this distance is going to do. Now what I'm going to do is they want the case closest points. They want you to return an array, an array, 2D array that has the case closest points. So I'm going to basically create my 2D array called to return and it's going to be size K because that's going to return that. I'm going to return that in the end. Okay, so now what am I going to do? Well, I need a max heap and I basically need to calculate the distances from every point and add it to my max heap with its index that it's at. So I'm going to do that here. So I'm going to do for i equals zero, i is less than points dot size, i plus plus is going to be going through every single point. Now what am I going to do? I'm going to calculate the distance I'm going to add. So to add distance, distance between every point to the origin. So that's going to be points at i. So that's going to get me the distance to the origin. Now I need to create my, now I've got to create my max heap and my max heap is going to contain the size of K that I'm going to add. So to do that, I'm going to create my max heap here, priority queue. This is how you create a max heap in C++. It's going to be, I have to pair the max heap with a pair each distance with its corresponding index of the point that it's at in the array so that in the end of my iteration I could actually get the actual points and not just the distance. So I have to use a pair. It's going to be double. Double is representing the distance. And then I'm going to pair it with its corresponding index. Okay. Then I'm going to use this vector. The priority queue is going to have pretty much, it's going to be a vector represent. Okay. Now, because it's a max heap, it's going to help pair. So how, because it's a max heap, I don't have to pass in a comparator because in C++ max heaps are actually the exact same thing. Like priority queues are normally in, are basically max heaps. So yeah, I don't have to pass anything for my comparator. But if I want to change it to a men heap, I have to have pass a comparator. Yeah. So in C++, the comparator to comparing pairs, they already compare based on the first value for each pair. So I'm good for that. So yeah, I don't have to pass a comparator for that. Okay. So I'm going to do if, if my max heap is size is greater than K, no, it's less than K. Then I'm going to add the corresponding distance to my max heap. So max heap dot push, add the corresponding distance to add distance and its corresponding index that it's at in the array. So I'm going to do that here. It's going to push the distance and its corresponding index into the array. And this is a pair. Yeah, push this, the pair of the index, the distance and the index. Yeah, this is what it does. Okay. Otherwise, what I'm going to do is I'm going to check if the top of the max heap. So what is my top of my max heap? So, okay. So I have to check if the distance that I'm going through in my array. Remember, we're looping through our array. We have to check if that distance is less than the distance for the top of our max heap. So going back to here, let's see again is greater to 20 less than the top of our max heap distance. Yes. And then we do that. So yeah, we have to check if the distance we're adding is less than the distance of the top of our max heap. And what is the distance of the top of our max heap? Well, okay. So our max heap is a pair corresponding the distance and the index of it. So the first value is going to be the distance of it. Our max heap is consistent of a pair of the distance to the origin and the index of each pair in the array. So the distance is going to be dot first, because that's the first pair first value in the pair. Yeah. So we're going to do that. If it's less than the top of our max heap, we're going to remove the first value of our max heap, do a pop, and then we're going to push the distance, push the distance and it's corresponding index to it. Okay. And that's basically how we're going to do it. That's basically how you do this problem. Now, when we get to the end, now I need to add the corresponding points. I need to get the points, not the distances of our max heap. So I'm going to go through all the values of my max heap. So while size greater than zero, I'm going to pop each value. And what I'm going to do is I'm going to add the each point to the array that I'm returning. So the array I'm returning has size of K. So I'm going to start at an index of zero. This is representing the position of each value I'm going to add to my array. So I'm going to do to return at index is going to equal to the first value of our max heap, which is max heap dot top. And that's going to be top dot second top dot second. This is the index. Remember, this is the index of each pair of each point of each distance. So what I'm going to do is I'm going to have to use this index to get the actual point points. So I'm going to use points at the specific index. And that's going to give me points at the specific index is going to give me the actual point. Okay. And to return. And then I'm going to do an index plus plus to go to the next index. And then at the end of this, it's going to return to return. And then let's submit the code. Okay, what did we do wrong here? Six, negative five, negative five for something came up that should not be the issue here. Do, do, do, do, do, do, do, do, do. Six, negative five, four, but expecting negative five, four, four, six. So we are not going through the right order. Let's see. Mm hmm. Okay. So let's hold up and just see something real quick. They have done this problem before, but let's see distance pop. It should be right. Oh, wait, I'm supposed to add it from the beginning to the end points at the index. Okay, let's try this. So doing K, what if we try push back? Because I feel like we're not adding it correctly to return. Let's see to return that push back points at wrong answer. Okay, that's the same thing as before. Let's see what are we doing wrong? Let me go scroll down to the first to pop push dot second. It should be right. It should be the same. Which what am I doing incorrectly? Max heap that size less than K max heap that push distance I K points distance as less than to the first maxi pop distance I J I do do do do do do do Max heap. Yep. Did I calculate double wrong? Let's see to return points K something is not something's not right. Okay. Maybe it's this part max heap. Okay, index. Let's do index. And index is going to equal to this max heap top dot second pop then went to return that push. What's the problem? Push back. Whoops. Hmm. Something's not right. Double double int pair double double int max heap and double and square root of point zero plus QIT point zero plus. What am I doing wrong? Let me check my accepted something max heap dot pop push distance. Oh, that's why should be a double distance returns a double. Okay. That's the reason why any got accepted. Yeah. So the problem what we were doing is that we were turning a calculate distance. It should have been a double not int. That was my fault. That's really my fault. But yeah, that's how you do this problem. Very calm. Subscribe. I'll check you guys later. Peace.