off99555's blog

By off99555, history, 8 years ago, In English

I'll try to explain the second problem here.

We have k buckets. Each bucket can contain no more than 2 objects.

Consider a very simple example. I have 5 objects of size 1 to 5 And 3 buckets. What we are trying to do is MINIMIZE the size of the largest bucket. The optimal solution is

Bucket 1: 5 Bucket 2: 4 1 Bucket 3: 3 2

So we have the lowest possible size of 5. How do we get at this value? Just simulate the process using greedy approach.

To get at the optimal solution, we need to balance all the objects into each bucket, we can do so by first putting the largest one in the bucket one and the next one into the next bucket. If there are more objects than the number of buckets, we simply put the next object in the last bucket and the previous ones.

So the overall size of each bucket is roughly the same. Thus, making the largest bucket the minimal size. This is just filling an array forward and backward. and find the maximum of the array.

The objects are already sorted in size so you don't need to sort it yourself, thus making the problem very fast to solve.

  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It appears that the official editorial is already available here. Thanks for sharing your thoughts.