• 1 day ago
Transcript
00:00Next question, which we are going to solve, is the name is Connect N Ropes. Ropes, or chains. For me,
00:05the question is that I have to connect N chains. First of all, I have given N Ropes with different
00:12lengths. Like if one rope is 4 meters, one length is 3 meters, one length is 2 meters, one length
00:18is 6 meters. My task is that I have to connect them all, and last, I have a single rope,
00:24single chains. This way, my cost is minimum.
00:28What is cost here? Cost is that the cost of connecting two ropes, that is equal to the sum of
00:35their lengths. That means, if I connect one x length and one y length, I connect these two ropes, then
00:41I have the final rope length, x plus y, and how much cost will come? The cost is also equal
00:46to x plus y. If I connect one length and five length ropes, then the cost of these two will
00:52be 6. I can say this one plus five which is equal to six.
00:56I have to connect my ropes with my final cost. For example, I have given my ropes like this. So,
01:07first of all, I will connect with 2 and 3. When I connect with 2 and 3, I will connect
01:13with the final rope and the final rope will be 5. And my cost will be 5. After that, I
01:21will connect with my 5 rope.
01:24I will combine this with 4. When the total length will be 9. How much will cost? Cost will be
01:369 plus 5 will be equal to 14.
01:42Then what will I do? I will connect the rope with the 9. I will connect it with my 6
01:47rope. We will combine it with a 15 size rope and now my cost will be 14 plus 15 which
01:53is equal to 29. My final answer will be that should be equal to 29. How do we solve this
02:01question? First, let's think logically.
02:05Every time, what is happening? If I have tried to connect these ropes, then every time, when I combine 2
02:12with 3, my cost is equal to 2. So, my cost is equal to 5. Then, when I combine 4
02:21with 4, then my cost is equal to 9. So, when I add cost to 9, I add cost to
02:279. Now, when I combine 6 with 6, then my cost is equal to 15. So, what is happening here?
02:3515 add ho gya. Can I say that which I had added in the beginning, I mean this 3 if
02:40I had an example which I had added in the beginning, this 3 is the cost in this cost. Why?
02:47Because the cost 5 is 5, it's 1 2 and it's 1 3. I show this cost to 1 level
02:53up to 1 level. That is 5, it's 1 3 and 1 2. So this 3 is the cost in
03:01this cost.
03:02After I look at the next level, one rope is 5 and one rope is 4. So 5 plus 4
03:07is 9. So if I say this is 4, this is 5 plus 4. And if I say this is
03:135 plus 4, this is 5 plus 4. And if I say this is 5, it is 2 and 3.
03:15So on the upper level, which rope is linked, the cost is linked again to the lower level. If I
03:23look at the lower level, it means 9 plus 6 is added. 9 means 5 plus 4 is added.
03:57Rope added first
04:01The cost is multiplied by the 5. The cost will be multiplied by the 5. The cost will be multiplied
04:09by the 5. The cost will be multiplied by the 5. The cost will be multiplied by the 5. The
04:14cost will be multiplied by the 5. The cost will be multiplied by the 5.
04:41This is the minimum length of the rope.
05:04I will add the ropes. Can I say that if I want to add the ropes to the minimum length,
05:11then I will add the ropes to these four. Which two ropes will be? It will be three and two.
05:17Three and two I have added so I will add the length of five. After that, I will have which
05:23ropes? One has four ropes, one has six ropes, one has five ropes.
05:29This 4 and 6 didn't connect, but 3 and 2 connected, I made a 5 length rope. Now, I will
05:36remove them from minimum 2. Minimum 2 will be 4 and 5. So, I combine 4 and 5, their cost
05:44will be 9 equal. Now, I have 6, I have 9 length rope. Now, I combine 2 again, 6 plus
05:529 equals 15. Now, I have 15 length rope. How much cost will be? 5 plus 9.
05:58plus 15, this value is equal to 29. So I have a final answer that will be 29. So what
06:05do I have to do every time? Every time I have to remove all the ropes, I have to remove
06:09minimum 2. I have to remove minimum 2, then combine them, then put it into total ropes, then remove minimum
06:162 again. Let's try to dry run this on another question. Let's look at another example. So I will use
06:22this question to solve this question, a priority queue. PQ is a work that PQ
06:28logic in the heap. So, what does heap keep on top? Min heap. So, pre-queue will always give me
06:37minimum value. So, first of all, I will put all the elements in priority queue. How will we arrange this?
06:43First of all, the minimum will come up. After that, I have 3, 3, 4, 6. I have some elements
06:50arranged in this form. What I will do is remove two pq. First, I will remove first minimum.
06:58After that, I will remove the second minimum.
07:02How much cost is 5? How much cost is 5? How much cost is 5? How much cost is 5?
07:05How much cost is 5? How much cost is 5? How much cost is 5? How much cost is 5?
07:51this will be equal
07:53Now I have pushed 11 here again. 11 has pushed 7 before and 11 has come back. Now I will
08:00remove minimum 2. So 7 and 11 will come back. It will be 7 plus 11. This is equal to
08:0618. And 18 will I have pushed back again. This is my single rope. How much cost is coming? Cost
08:12is coming 5 plus 7. This is equal to 12. 12 plus 11. This is equal to 23. Plus 23.
08:21This is equal to
08:2241. So, the minimum cost in this question is 41. Don't confuse me this approach from greedy. Greedy means that
08:32I would directly sort it. If I would directly sort it, although I would have to take a minimum, directly
08:38sort it, then I would have 2, 3, 3, 4, 6. What does greedy approach mean?
08:43First, what do you do? 2 and 3 combine them, means they both combine them, cost is 5. Then, 5
08:48and 3 combine them, cost is 8. Then, 8 and 4 combine them, cost is 12. Then, 12 and 6
08:58combine them, cost is 18.
09:02This will be equal to 30. This will happen to me. 13 equals the final value that will be 43.
09:07The 43 value that will come from greedy, this is more as compared to this cost. So I have the
09:13way to follow the priority. Logically,
09:17This is the first time when students see this question, it will strike that I will just sort it, and
09:22I will just sort it directly. But this question is not the same. For this question, I have to use
09:28the priority queue because every time it is not necessary to combine two small ropes, which are my big rope,
09:35combine the rest of them as small. Because it can be such that many small ropes exist, which I have
09:41to combine first and then I have to combine first. So I have to use the priority queue approach.
09:47In the main function, what did I have given? I have given my ropes values. What will I do? I
09:51will create a priority queue in which I will store my integer values. I will name it pq is equal
09:57to new priority queue. Now, we are going to store all the ropes values in pq.Length i++. I will
10:11do pq.add ropes of i. What do I have to do?
10:17I will take a variable with cost which I will initialize with zero. I will run in a loop. This
10:22loop will run until I run until my priority queue will not remain in single rope. So until my pq
10:28.size is greater than one. Until I have to combine the ropes. So first I will remove minimum. What will
10:36be minimum? Minimum will be pq.remove. Whatever value is removed will be minimum rope. Then what will I do?
10:45I will remove second minimum.
10:46Minimum To Minimum To Minimum To Minimum To Minimum To Minimum To Minimum To Minimum To
11:08I mean, the two values were removed, now a single value will be added, which is my new rope's value.
11:14Then it will be internally sorted, as it will be added, the minimum of the heap will be added. Then
11:21we will remove the minimum from the next level and the minimum of the two will be added. Then we
11:25will add the cost from both of them and add the final rope to PQ. What will I do in
11:31the last one? I will print the cost. My cost of connecting.
11:37n ropes is equal to cost. Let's do this save and run. Cost which I have, that is equal to
11:4741. So I have to solve this question in this way. This is not a difficult question, it is a
11:51very easy level question. We have written a little bit of code. And you have noticed why the priority queue
11:56is so easy. Things are in a sorted fashion.