How much does one blank dvd+r cost?

Google Interview Questions: What is the best algorithm to find the minimum cost for joining a set of rods to produce one rod, where in a single move you can join any two rods with cost of this operation equal to the length of the new rod?

  • Example: Suppose the rod lengths are { 1,2,3 } Then optimal way would be: 1) merge 1 and 2 => {3,3} cost = 3 2) merge 3 and 3 => {6} cost = 6 Total cost = 9. Extra: How will the algorithm change if in a single operation you can merge atmost K set of rods together(with the cost again equal to the length of new rod)? My Solution: Insert the rod lengths into a priority queue and then while the queue contains more than 1 element, pop two minimum elements, join them and add it back to the queue. This algorithm seems right but I am unable to prove its correctness.

  • Answer:

    Your algorithm is correct, greedily merging two smallest rods is optimal. This problem is the optimal prefix code (http://en.wikipedia.org/wiki/Huffman_coding) in disguise. The answer is sum(original rod length * number of times this rod was involved in a merge). To see their equivalence, draw a merging tree: a tree that shows the order in which you merge the rods. This will turn out to be the same tree as the Huffman code tree, with original rods corresponding to different characters, and original rod lengths corresponding to their frequencies. Here's the outline of the proof of correctness: If you knew the optimal merging tree and only had to assign original rods to its leaves, this can clearly be done greedily: the longer the rod, the smaller depth you want. Thus, it is optimal to put the shortest rod into the deepest leaf. In any merging tree each node that is not a leaf has two children. Therefore, the deepest leaf has a sibling. As there are at least two nodes with maximum depth, the second shortest rod will be placed at the same depth as the shortest rod. If we have a bunch of leaves that all have the same depth, we can reorder their rods without changing the value of the solution. Thus, the two shortest rods can be placed into two sibling leaves. This proved that there is always an optimal solution where you start by merging the shortest two rods. You can repeat the above argument with the n-1 rods you have now, and so on until just one rod remains.

Michal Forišek at Quora Visit the source

Was this solution helpful to you?

Other answers

A similar problem appeared in the ACM-ICPC Kanpur Regions Onsite 2013. You can have a look at the question and the solutions by various teams here: http://www.codechef.com/KAN13ACM/problems/KAN13F

Divanshu Garg

This is also the result of the permutation inequality. The first 2 rods will appear in n-2 costs, the one after in n-1 and so on. The final cost will be (n-2)*r1+(n-2)(r2)+(n-3)*r3....1*r1. By the permutation inequality since n-2>=n-3>=1 it is minimal when r1<=r2<r3...<=r1. (a short proof would be to otherwise switch 2 that weren't in opposite order compared to their coefficients and see that we lower the total sum).

Mike Bojan

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.