So I am solving this problem given as:
There are bags carrying weights from 1 to 3 units and I have to carry them from point A to point B. Weights are given of bags in array. All weights are less than 3 unit.
weights = [1, 1.78, 2.2, 2.73, 3]
So I have to make the trips carrying the bags should not cross the total weight greater than 3 unit. In order to that I have to make minimum number of trips.
Example: weights = [1.01, 1.99, 2.5, 1.5, 1.01]
I can carry all bags in 3 trips minimum as:
[1.01 + 1.99, 2.5, 1.5 + 1.01].
Means how to determine the minimum no. of trips to carry the weight bags from point A to point B?
What logic can be applied?
One approach is to loop through a sorted list and always pick out the biggest number first.
Then you loop through the remaining elements and combine it with the largest number, where the combined sum is less than your target.
Since the largest number should always be the hardest to "pair", this should give you the optimal number of trips.
However this approach doesn't distribute weight evenly. It will favor packing pairs as big as possible.
Here is a simple implementation: