- 10 hours ago
Transcript
00:00Next is the question we are going to talk about. This question is the weakest soldier. We have to find
00:07out what is given. M cross N size is a binary matrix. Binary matrix is a matrix that only one's
00:14and zero's. One means that one soldier is standing there. Soldier is standing there. Zero means that one civilian is
00:22standing there. So, this is a position for me, either standing there or standing there. Zero means that one civilian.
00:29So soldiers always come from civilians. So first all soldiers will stand. If I talk about this row, then soldiers
00:39will be then civilians. So soldiers will be then soldiers. So soldiers will be then soldiers. So soldiers will be
00:45then soldiers. So soldiers will be then soldiers. So soldiers will be then soldiers. All the ones will appear to
00:51left of all the zeros in each row.
00:54Now what can I do? I can define each row as a soldier and civilian, how strong and how weak
01:02a row will be. A row i will be weak when one condition will be true. So if row i
01:10will be less than soldiers, then they will be weak. Like this row, if j is equal to
01:171 is equal to 1 and this i is equal to 0. J is equal to 1 and there are
01:21more soldiers. So, this i is equal to 0 and this i is equal to 0 and this i is
01:27equal to 0 and this i is equal to 0 and this i is equal to 0 and this i
01:34is equal to 0 and this i is equal to 0 and this i is equal to 0 and this
01:35i is equal to 0 and this i is equal to 0 and this i is equal to 0 and
01:39this i is equal to 0 and this i is equal to 0
01:43the row is weak. So I can judge to compare two rows to which one is weak and which one
01:48is strong. On which basis are strong weak? On the basis of soldiers. On which soldiers are weak, they will
01:54be weak. And if soldiers are the same, which one is weak, they will be weak. So this is a
01:59condition. And I have to find the K weakest rows. K value I always know is less than.
02:05M will be total number of rows. I will find out the k-weakest rows. If I talk about this
02:11example, given example, then the answer will be my row 0 and this is my weakest and second weakest is
02:20my row 2.
02:22So, if I compare this row, I also have less soldiers in this row. So, this is weak. If I
02:29compare it with this row, I have equal number of soldiers, but the index is less. So, this is weak.
02:35If I compare it with last row, I have equal soldiers in this row, but the index is i less
02:39than j. So, this is weak. So, this row is my weakest row. So, 0 is my weakest row.
02:46So when you compare it with this row, the soldier is the same, but its index is less than g.
02:52So this will be my second weakest. So the answer will be 0 and 2.
02:57Now this question is a little big but this is actually a very easy level question. And I hope that
03:02as far as we have studied heaps and priority cues, we have studied a lot of good things. After that,
03:08we can answer such questions easily. So you can also try to pause the video and try to solve the
03:14question yourself. Otherwise, we are still discussing the approach. If I talk about the approach, I have given MN and
03:21K value. If I have my rose,
03:24So, first of all, I will write the index. This is, my index i is equal to 0, this is
03:30i is equal to 1, this is i is equal to 2, this is i is equal to 3. Can
03:34I say that what I have to do with sorting? What do I have to do with them? I have
03:38to make minimum out of them. Minimum, that is weakest. Weakest out of the logic. If the amount of soldiers
03:48is stronger, they are stronger.
03:54or if I want to write the opposite, if someone's soldiers' amount is less than a week, then if the
04:01soldiers' amount is less than a week, then if the soldiers' value is less than a week, then if the
04:09soldiers' amount is less than a week, then if the soldiers' amount is less than a week, then if the
04:11soldiers' amount is less than a week, then if the soldiers' amount is less than a week,
04:15So I can add all rows in sorting order. Or if I use the priority queue, then I can create
04:22this logic in Q. How? I will create a class named row and store the soldier's count in each row.
04:29What is the soldier's count in the first row. What is the soldier's count in the row? It is equal
04:34to 1. What is my index? Index is equal to 0. Then row 1. What is the soldier's count in
04:40the row? 1, 2, 3, 4. And the index will be here.
04:441K equal. Row 2 में soldier का count हो जाएगा 1, index की value हो जाएगी 2. Row 3 में
04:51soldier का count हो जाएगा 1, index की value हो जाएगी 3K equal. अब मुझे इन rows को अगर sort
04:59करना है, तो sorting का यह logic रहेगा, जिसमें soldiers कम है, वो उतना ही जादा weak हो जाएगा. तो
05:03यहां से तो मुझे साफ साफ दिख रहा है, मेरी जो यह वाली row ह
05:12sabse strong ho jayegi yamai isko lik dheti hon yaha par agar mai ascending order mein sort karne ki koishish
05:18kar rhi ho to ro 1 yaha par a jayegi uske baad mai ro 0, ro 2, ro 3 ko
05:23compare karu ab in tino mein soldiers ki amount same hai yani tino ke anndar soldiers ka jo number hai
05:29that is equal to 1 to index jiska bada hooga wo utni strong ho jayegi index bada hai index is
05:35equal to 3 ka
05:36So second strongest will be R3. Then compare R0 and R2. So which index is bigger than R2. So this
05:44will be strong. Then R2 will be less strong or weak. That is my R0. So if I put some
05:53priority Q in, then R0 will be R2, then R3 will be R.
06:00I can write a proper logic in this basis, which we can write in the class and use the comparable
06:05interface. And when I put it in 1-1, then R0, then R2, then R3, then R1, then R1, then
06:13R1. Now, if I put it in case equal to 2. If I put it in first, then R0 will
06:18print it, and then R2 will print it. And then R2 will print it. This whole logic will convert in
06:23code one time.
06:25I have my whole army information given in 2D array and I have k given. First of all, I have
06:32to make a class. Static class, I give it a name, row. Now I have to sort the row together,
06:38so I will implement it with my comparable interface for my row objects. In every row, I store the count
06:47of soldiers. I store the count of its index.
06:54The constructor bana lathay public row. Is mein aega meri soldiers ka count and meri index ki value. This dot
07:03soldiers is equal to soldiers and this dot index is equal to my index. Ab override karke mein compare to
07:10ko implement kar lathay ho. Public int compare to. Yaha par a jayegi meri row r2. May return kar dhungi.
07:22Here I can change the logic in compare2. Compare2 is a logic to compare two objects. Now I know that
07:29when it is equal, compare2 is what returns? Compare2 is 0 returns. You want to keep row1 and keep row2
07:37and keep row2. It doesn't need to be equal. But this time don't do that. Compare2's logic will say that
07:44if I count the soldiers of one object,
07:50If my second object is equal to R2 and row 2, if soldiers count is equal, then compare on the
08:01basis of index. So return this.index minus R2.index. If the index is small, it will take a small
08:09value. If the index is small, it will increase in ascending order. If soldiers count is not the same, then
08:17I have to sort on soldiers' basis. Then I will return this.soldiers-r2.soldiers. What did I do? The
08:25logic of comparison is on two parameters. The first parameter is that if soldiers' count is the same, then you
08:32will sort on the basis of the index. And soldiers' count is not the same, then you will sort on
08:36the basis of the soldiers' basis. So this is my compared to logic. I will save it. In the main
08:41function, I will make a priority queue. Priority queue of
08:46my rows. I will name it pq is equal to new priority queue. Now I am going to store all
08:56the information. i less than army dot length i plus plus. First of all, I am going to count the
09:05soldiers inside my row. We can run another loop here. int count is equal to zero for int j equals
09:15zero j less than
09:16army of zero dot length j++. What will I do every time? If my army of ij equals equals 1.
09:27In that case, I will add 1 to count, not add 0 to count. Then I have used ternary operator.
09:34Here in count, I will add this value every time. I hope this little line will come up with logic.
09:41I have not used if, we can also use if.
09:44If my soldier's value is 1, my soldier is standing there, so I will add 1 or not, I will
09:49add 0. What will I do in last? I will add in PQ, I will add my new row, which
09:54will count in soldiers, and the index value will be equal. So I will add all the rows in PQ.
10:02What will I do in last? I will print K-Weekest. So why do I do by default priority? I
10:07will sort in ascending order. So all the rows will sort in ascending order by default.
10:13So I call it for i equals 0, i less than k, i plus plus. I print every time I
10:19print your row index which will be r plus pq.remove.index. Let's save and now run. I have two
10:32weakest rows. The first value is r0 and second value is r2.
10:39So, in this way, we can remove the weakest rows from there using this priority queue. In fact, if we
10:47want, it's not necessary to write such a big function for us. We can write our functions here and define
10:54the logic here in the form of comparators. But because we all are studying the first time priority queue, so
10:58first time comparator, comparable things are studying. So, I don't want to know any shortcuts. We are using the override.
11:06So, as a beginner, we are in this concept.
11:09this concept is correct and you can use it anywhere else. Yes, there are some shortcuts to write this same
11:13thing, which you can explore on Google, how you can use the comparators for comparing two objects. So, you can
11:21write them directly here, without the whole function.