Define the cost of adding two numbers together as its sum. For example, adding 1 + 2 has cost 3. When we want to add two numbers, the cost is easy to compute, but when we want to add more numbers, there are various ways to do the additions, by choosing which two numbers to add next. While all of them will give the same sum, not all of them will give the same total cost.
For example, suppose we want to add the numbers 1, 2, 3, and 4. There are various ways to do this. As examples:
There are 18 ways to add those four numbers. Out of all of them, the minimum cost is achieved when we add ( ( ( 1 + 2 ) + 3 ) + 4 ) , with cost 19.
There are approximately 1 . 3 7 4 × 1 0 2 8 4 ways to add 100 numbers. What is the minimum total cost of adding these 100 numbers ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let me try giving the greedy proof since I had a hard time convincing myself to "just code it!".
What makes us to not choose the two minimum number each time? Easy, because we might get an even smaller number afterwards (think of why we don't always take the maximum denomination in the coin change problem). But is it possible? If I do not sum up the two minimum number (which equal to x), in later summations none will add a cost smaller than x. Hence we can just greedy.
The previous version of this problem is wrong; there aren't ( 2 1 0 0 ) ⋅ 9 8 ! ways to add 100 numbers together, there are ( 2 1 0 0 ) ⋅ ( 2 9 9 ) ⋅ … ⋅ ( 2 2 ) ways. Because the first one was the one presented, I initially thought you had to add the previous sum with one of the numbers; that is, I thought ( ( 1 + 2 ) + ( 3 + 4 ) ) was not allowed (because it adds 1 and 2 together, then puts it off and adds 3 and 4 together first).
Log in to reply
I think Pi Han has reverted the edits. Does the problem look acceptable now?
Log in to reply
When I read the problem, it was the first version I stated; I then edited it to show the second version, the current one. I'm not sure if there was a previous version before.
Problem Loading...
Note Loading...
Set Loading...
Relevant wiki: Heaps
A way to visualize this problem is to see it as a list of n numbers being transformed to a list of n − 1 numbers at each step. That is, we are taking away two numbers and adding back their sum.
Now, at each step, the cost is the sum of the two numbers we'll add. It is also the number that goes back into the list.
In order to minimize the total cost, we'll follow a greedy strategy. At each step, we'll add the two least numbers. (This should be intuitively obvious now, but if someone can prove this, it'd be helpful.)
So, how do we efficiently get the two least number at each step? Sorting each time works, but luckily there is a more efficient solution. Heaps .
So, that get's the job done in O ( n lo g n ) time.