• 1 day ago
Transcript
00:00Next is the question that we are going to solve. It's called Nearby Cars. I have to search some cars.
00:06I will give N points which are 2D plane. This is my 2D plane. This is my x-axis and
00:13this is my y-axis. There are some coordinates or points that I have given. The first point that I
00:19have given is 3,3. I divide my number line at 5.
00:271,2,3,4,5 and 1,2,3,4,5 to understanding ke liye 3,3 kaha hai 3
00:34,3 mera point laye karta hai yahaan per this is my c0 uske baad diya hai c1 coordinate 1.1
00:39ye hai 5,-1 peh to 5,-1 kuchhi yahaan per hooga this is my c1 for tisra point diya
00:47hai c2 which is minus 2,4 minus 2,4 laye karta hai yahaan per this is my c2 mera
00:54kaam hai ki mujhe apne aas pas
00:57K-nearest cars. These are all the coordinates. These are car's coordinates. N cars are standing on my 2D plane.
01:04Here my first car is standing. Here my second car is standing. Here my third car is standing. And where
01:09I am standing? I am standing on the origin. So we are standing on our position. That is at the
01:14origin. Origin's position is 0,0. I have to print the nearest K-cars. N cars is given to me.
01:22And I have given K is less than N.
01:24or less than or equal to be. K value is always less than n from n from n from n
01:27from n from n from n from n from n from n from n from n from n from n
01:29from n from n from n from n from n from n from n from n from n from n
01:29from n from n from n from n from n from n from n from n from n from n
01:34from n from n from n from n from n from n from n from n from n from n
01:42from n from n from n from n from n from n from n from n from n from n
01:43from n from n from n from n from n from n from n from n from n from n
01:46from n from n from n from n from n from n from n from n from n from n
01:47from n from n from n from n from n from n from n from n from n from n
01:47from n from n from n from n from n from n from n from n from n from n
01:48from n from n from n from n from n from n from n from n from n from n
01:50from n from
01:52There is no point origin, 0,0. And I have no point that is on x,y. Let's say the
02:00distance of x-axis is equal to x-axis and the height of this distance is equal to y. So
02:06how much distance between both of these two is equal to this line? This is equal to under root x
02:14-square plus y-square. This is the distance. Or I can say this, the distance of the square is equal
02:21to x-square plus y-square.
02:22This is the distance of square value. If you put it under root, this is the whole distance of value.
02:29So it is very easy that if you have given origin or point in each other, the line will become
02:33the distance between each other. So we have to find a simple formula. Let's count for our first line. Let's
02:40say this is line 0. For line 0, this distance will be equal to C0, meaning 3 square plus 3
02:45square. Can I say this? This is under root of 9 plus 9 is 18.
02:50You don't need to do under-root. You can count it as 18. If I ignore under-root because all
02:56the values are under-square, then all the values will be positive. The number is so big, the under-root
03:02is so big. That means the square root is so big. That's why I want to compare the square root
03:06or compare the distance inside. Both will give the same answer. So what do I do? The square root will
03:12be much mathematical. So I remove the square root. I compare the square root directly from the square. That means
03:18from x square plus y square.
03:20Then I will see the distance from the other line. I will see the distance from C1. This will be
03:39equal to 26. Then I will see the third line. The third line means origin from here. How much will
03:46this be L2? This will be L2. The distance will be L2.
03:51These are my x's values and these are my y's values. x's value square is minus 2 square plus 4
03:57square. This will be equal to 4 plus 16 equals 20. Now in this case, the nearest near is the
04:03past. The past is the same thing that the distance is less. If one thing is 5 and one thing
04:10is 10, then obviously the 5 will be near. So I have the k's nearest. So in this case, the
04:16first nearest, the past is this node.
04:20I mean C0. C0 why? Because it's the distance is the lowest. If it's the distance of the square is
04:25the lowest, then the under root will be the lowest. That's why 18 is the car 0 is the lowest.
04:31And the second one is the lowest car. That means C2. This car will be near rest and this car
04:39will be near rest. So I have to print these two cars as my output. So my answer will be
04:44C0 and C2.
04:46This question is not enough. This question changes its own form. Can you ask directly to the nearest K points
04:54to the origin?
04:57directly you can ask the question in this form, so the logic of both of you is the same. In
05:02this question, you will also ask the nearest cars, the nearest horses, if you are standing on the origin or
05:08the nearest horses, tell them. Or if you are standing at any point at any point, tell your nearest restaurants,
05:16the nearest restaurants, if you are hungry, eat food soon, so which restaurants you can go to? So the logic
05:21of this question is the same, we will use the priority cues to solve it.
05:27question statement can change. So what do we do? We always need to take care of logic. So what do
05:32I do in logic? I use this every time in the approach that if I am standing on the origin,
05:38then every given car, I will remove the distance of the car's square. The distance of the square will be
05:4318, the distance of the square will be 26, the distance of the square will be 20 equal. Then what
05:49do I do? Why do I put all these coordinates in the priority queue?
05:53priority q के अंदर मैं bundle डाल दूं c0 के लिए उसका distance जाएगा is 18 c1 के लिए उसका
06:00distance जाएगा is 26 and c2 के लिए उसका distance जाएगा is 20 priority q क्या करता है priority q
06:06by default maximum priority को आगे रखता है क्या मैं कह सकती हूँ अगर मेरे लिए मुझे minimum distance वाली
06:12nearest car find out करनी है तो मेरे लिए priority है distance distance का square जितना कम होगा उतन
06:21high. So what will I do? All of my objects will be made by my cars. I will push my
06:28priority with distance. Why will I give my priority? It will give me the maximum priority. Maximum priority means minimum
06:36distance. So internally I will sort it when I remove the elements. First I will have C0 with 18. Then
06:44I will have C2 with 20. Then I will have C1 with 20.
06:4826. So, either I can sort this question or not, I can simply do it with priority queue. You can
06:55also do sorting. If you want to use priority queue, you can also use priority queue. Both of you sort
06:59your elements. And I have to sort on what basis? I have to sort on the basis of my distance.
07:05So, here I have a class, a classroom name. I have all my points. And I have K value. What
07:12do I do? I will make a class. I will name it static class point.
07:17I will store the x-coordinate and store the y-coordinate and store the distance. So, let's make a constructor
07:26public point which will be x and y and distance square. This.x is equal to x. This.y is
07:38equal to y. This.distance square is equal to distance square.
07:45What do I have to do? I have to sort every point. Using what? Using the distance of the square.
07:50So this is my comparison factor. When you compare the class of objects, what do you do? We have already
07:56learned that we implement the comparable interface. Why do you do the comparable interface on which basis? On the point
08:03basis. So I will implement the comparable interface. Here I will override the compare to function. PublicInt. CompareTo.
08:14Compared to, if there is another point that comes in, P2, then the value should be sorted on, on the
08:20basis of the distance, which is less, it will come first. So I will write directly, return this.distance2-P2
08:30.distance2. What will happen with this line? I will have the sorting, it will be in ascending order. If I
08:37have to do this sorting in descending order, then there is nothing. Or I will reverse this logic here, then
08:41it will be in the priority queue. I will have my collections.
08:44I will pass the reverse order. So let's do this save. So I can compare my points in this way.
08:51Now I am going to create a priority queue. Priority queue which I will store all the points in which
08:55I will store all the points. PQ is equal to new priority queue. I will store all the points. I
09:05less than pts.length I++. I will add in PQ every time. New point which I will pass in which
09:14I will pass my pts of I is 0.
09:16This will be my x-axis coordinate. pts of i1. This value of y-axis will be the value of
09:23y-axis. And then the distance of square. What will be the distance of square? I will make a variable
09:28here. The name of distance of square. The distance of square is x square. This will be x into x.
09:38x square plus y square. This will be y into y. This will be x square. This will be y
09:46square.
09:47So here I will add my distance square, this value value. So I have added points in the priority queue.
09:53Now I have to print nearest k. So I will print for nearest k cars. I will print the loop
10:01for int i equals 0, i less than k, i plus plus. This loop k times will go out. I
10:07will print every time and remove it from pq. Here I will add my c. What will happen next time?
10:16k times, i.e. two times this loop will go and every time the priority queue will remove the minimum
10:20element from there. How do you know? It will compare my cars to the distance. Let's do this, save and
10:30now run. I have my object in output because what will I give? I give my object to remove the
10:37pq and I give my object address. So I can store another parameter here, that is my index.
10:45I mean, I will save my original index. This.index is equal to index. And here I will store my
10:53distance square and my i. So, I can print .index here. Index is C0, which means this point. And C2,
11:06which means this point. These are the two cars that are in points. These are my nearest cars. So, we
11:12will solve using priority queues and sorting my objects of a class.