How to partition an array of integers in a way that minimizes the maximum of the sum of each partition?

自闭症网瘾萝莉.ら 提交于 2019-11-26 12:45:41

问题


The inputs are an array A of positive or null integers and another integer K.

We should partition A into K blocks of consecutive elements (by \"partition\" I mean that every element of A belongs to some block and 2 different blocks don\'t contain any element in common).

We define the sum of a block as sum of the elements of the block.

The goal is to find such a partition in K blocks such that the maximum of the sums of each block (let\'s call that \"MaxSumBlock\") is minimized.

We need to output the MaxSumBlock (we don\'t need to find an actual partition)

Here is an example:

Input:

A = {2, 1, 5, 1, 2, 2, 2}
K = 3

Expected output:

MaxSumBlock: 6
(with partition: {2, 1}, {5, 1}, {2, 2, 2})

In the expected output, the sums of each block are 3, 6 and 6. The max is 6.

Here is an non optimal partition:

partition: {2, 1}, {5}, {1, 2, 2, 2}

The sums of each block in that case are 3, 6 and 7. The max is hence 7. It is not a correct answer.

What algorithm solves this problem?

EDIT: K and the size of A is no bigger than 100\'000. Each element of A is no bigger than 10\'000


回答1:


Use binary search.

Let max sum range from 0 to sum(array). So, mid = (range / 2). See if mid can be achieved by partitioning into k sets in O(n) time. If yes, go for lower range and if not, go for a higher range.

This will give you the result in O(n log n).

PS: if you have any problem with writing the code, I can help but I'd suggest you try it first yourself.

EDIT:
as requested, I'll explain how to find if mid can be achieved by partitioning into k sets in O(n) time.
Iterate through the elements till sum is less than or equal to mid. As soon as it gets greater than mid, let it be part of next set. If you get k or less sets, mid is achievable, else not.



来源:https://stackoverflow.com/questions/32437090/how-to-partition-an-array-of-integers-in-a-way-that-minimizes-the-maximum-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!