tesco
11-11-2010, 11:22 PM
This is a good one for any coding experts that are here (if we have any :O)...
I need this in PHP but I don't need you to write the code, I just need the logic of how this will work then I could probably write it myself.
Lets say I have an array of values like so...
10, 56, 78, 25, 18, 1, 8
I want to group these together so that each group is less than or equal to 100, with the least number of groups possible.
Well this is what the groups would be in an efficient sorting:
Group 1 = 10, 56, 25, 1, 8
Group 2 = 78, 18
There are other combinations of course, but the point is that we have 2 groups.
If I were to just simply loop through them and add the values, and start a new group when i reach the max (100), that will result in this:
Group 1 = 10, 56
Group 2 = 78
Group 3 = 25, 18, 1, 8
Or if I were to sort them, then loop through them like above, this would be the result:
1, 8, 10, 18, 25, 56, 78
Group 1 = 1, 8, 10, 18, 25
Group 2 = 56
Group 3 = 78
So now hopefully you see what the problem is...
What is the logic behind this that I could sort them like my "efficient sorting" example?
I need this in PHP but I don't need you to write the code, I just need the logic of how this will work then I could probably write it myself.
Lets say I have an array of values like so...
10, 56, 78, 25, 18, 1, 8
I want to group these together so that each group is less than or equal to 100, with the least number of groups possible.
Well this is what the groups would be in an efficient sorting:
Group 1 = 10, 56, 25, 1, 8
Group 2 = 78, 18
There are other combinations of course, but the point is that we have 2 groups.
If I were to just simply loop through them and add the values, and start a new group when i reach the max (100), that will result in this:
Group 1 = 10, 56
Group 2 = 78
Group 3 = 25, 18, 1, 8
Or if I were to sort them, then loop through them like above, this would be the result:
1, 8, 10, 18, 25, 56, 78
Group 1 = 1, 8, 10, 18, 25
Group 2 = 56
Group 3 = 78
So now hopefully you see what the problem is...
What is the logic behind this that I could sort them like my "efficient sorting" example?