What is the fastest way, given a list of lists of different dimensions, to find all the smallest subsets with sum larger or equal to a specific value?
So that if the set and the value are
A = [[1],[1,2,3,4],[5,6],[10],[1000,12],[11]]
value = 10
The solution would be
res = [[10],[11]]
or again:
A = [[1,10],[1,2,3,4],[5,6],[10,10],[1000,12,1],[1,10,11]]
value = 10
res = [[1,10],[10,10]]
Thanks in advance
You can use the following python code: the code finds a list of lengths in A then checks the sublists in ascending order, i.e. sublists of size 1, then size 2, and then size 4. Note that the code does not check larger sublists if an smaller sublist is already included in the result.
A = [[1],[1,2,3,4],[5,6],[10],[1000,12],[11]]
value = 10
lens = list(set([len(i) for i in A])) #result = [1,2,4]
res = []
for l in lens:
if len(res) == 0:
for i in A:
if len(i) == l:
if sum(i) >= value:
res.append(i)
print(res) #result = [[10], [11]]