Solution: Connecting n Pipes with Minimum Cost
Review the solution to connecting n pipes with minimum cost in detail.
We'll cover the following...
We'll cover the following...
Solution: Sorting
Explanation
If you look at the animation presented in the previous lesson, you will notice that the lengths of the pipes that are picked first are included iteratively (i.e., each time). Therefore, we want them to be as small as possible. Now, let’s be greedy!
First, we initialize the priority queue with the pipes. The priority queue ensures that the two smallest length pipes are selected in every iteration. In this way, in every iteration, ...