The Partition problem

Viewed 14951

I have set of non-unique numbers and would like to partition those numbers into K partitions such that sum of numbers in each partition is almost equal . Assume I have following set.

{1, 2, 3, 4, 5, 6, 7, 8, 9}

Using Linear partition algorithm I get following partitions when K = 3

{ 1  2  3  4  5 }
{ 6  7 }
{ 8  9 }

Which is expected, but since this is linear partitioning algorithm , any change in the order of the input set will change the partitions also, which I want to avoid.

Difference of Sum of elements for each partition should be minimized. In above example Sum of each partitions is 15 , 13, 17

for following input it does not work.

{10, 20, 90, 100, 200}

Linear partition algorithm gives me following

{ 10  20  90  100 }
{ 200 }

But correct answer should be

{ 10, 200 } { 20, 90, 100 }

4 Answers

LeetCoder have worked on the same problem definition (and solution) provided by Steven Skiena. Only thing is that he talks in C++, so it becomes some what easier to grasp.

Related