Huffman Coding Application - Gold Bar Cutting Board

A gold bar cut in half costs the same amount of copper as the length.

For example, a gold bar with a length of 20 will cost 20 copper plates no matter how long it is cut in half.

A group of people want to divide the whole gold bar, how to divide the most copper plate?
For example, given an array {10, 20, 30}, representing a total of three people, the length of the entire gold bar is 10+20+30=60.

The bar is divided into three parts: 10, 20, and 30. If you first divide the gold bar of length 60 into 10 and 50, spend 60 and then divide the gold bar of length 50 into 20 and 30, and
spend 50 to spend a total of 110 copper plates.
However, if you first divide a 60-length gold bar into 30 and 30, and then spend 60 to divide a 30-length gold bar into 10 and 20, it costs 30 to spend a total of 90 coppers.
Input an array, return the minimum cost of splitting.

 

First build a small root heap of the array, then take the two smallest values ​​from it, sum, and then put the sum back into the small root heap, and then continue this operation.

Until there is one value left in the root heap, it is the desired value.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324925272&siteId=291194637