I have a certain number of boxes in a certain order and the number of weights in a certain order. Scales can have different weights (for example, the weight can be 1 kg, another 2 kg, etc.). I want to put the scales in the boxes so that they are evenly distributed as possible weight. I must take the weights in the order in which they are given, and I must fill in the boxes in the order in which they are indicated. That is, if I put the weight in the field n + 1, I can not put the weight in the field n, and I can not put the weight m + 1 in the field until I put the weight m in the field.
I need to find an algorithm that solves this problem for any number of boxes and any set of weights.
A few C # tests with xUnit (Distribute is a method that should solve the problem):
[Fact] public void ReturnsCorrectNumberOfBoxes() { int[] populatedColumns = Distribute(new int[0], 4); Assert.Equal<int>(4, populatedColumns.Length); } [Fact] public void Test1() { int[] weights = new int[] { 1, 1, 1, 1 }; int[] boxes = Distribute(weights, 4); Assert.Equal<int>(weights[0], boxes[0]); Assert.Equal<int>(weights[1], boxes[1]); Assert.Equal<int>(weights[2], boxes[2]); Assert.Equal<int>(weights[3], boxes[3]); } [Fact] public void Test2() { int[] weights = new int[] { 1, 1, 17, 1, 1 }; int[] boxes = Distribute(weights, 4); Assert.Equal<int>(2, boxes[0]); Assert.Equal<int>(17, boxes[1]); Assert.Equal<int>(1, boxes[2]); Assert.Equal<int>(1, boxes[3]); } [Fact] public void Test3() { int[] weights = new int[] { 5, 4, 6, 1, 5 }; int[] boxes = Distribute(weights, 4); Assert.Equal<int>(5, boxes[0]); Assert.Equal<int>(4, boxes[1]); Assert.Equal<int>(6, boxes[2]); Assert.Equal<int>(6, boxes[3]); }
Any help is much appreciated!