Let's say I have a list of 10 elements and max_sum:
items = [1, 2, 4, 4, 10, 10, 15, 18, 21, 22]
max_sum = 30
I want to group the elements in items, and I want to find the minimum number of groups, provided that the sum of the elements in each group is less than the specified value max_sum, where all the elements are itemsless max_sum.
The general idea of ββthe algorithm:
- initialize 1) empty list
new_group, 2) float space_leftin group=(max_sum - sum(new_group)) - find the largest item like item <= space_left
- add the largest item to
new_group - update
space_left - remove item from
items - once
min(items)> space_left, start over - count cycles to find the minimum number of groups
So, for given values, this algorithm will give 4 groups:
[22, 4, 4]
[21, 2, 1]
[18, 10]
[15, 10]
, , , / . !