I'm trying to generalize the algorithm Paul Hankin provided in Maximizing the overall sum of K disjoint and contiguous subsets of size L among N positive numbers such that the solution is not limited to each subset being exactly size L and where the goal is not to maximize the overall sum, but to return the set with the largest subsets possible.
Spelling out the details, X
is a set of N
positive real numbers:
X={x[1],x[2],...x[N]} where x[j]>=0 for all j=1,...,N
.
A contiguous subset called S[i]
consists of up to L
consecutive members of X
starting at position n[i]
and ending at position n[i]+l-1
:
S[i] = {x[j] | j=n[i],n[i]+1,...,n[i]+l-1} = {x[n[i]],x[n[i]+1],...,x[n[i]+l-1]}, where l=1,...,L
.
Two of such subsets S[i]
and S[j]
are called pairwise disjoint (non-overlapping) if they don't contain any identical members of X
.
Define the summation of the members of each subset:
SUM[i] = x[n[i]]+x[n[i]+1]+...+x[n[i]+l-1]
The goal is find contiguous and disjoint (non-overlapping) subsets S[1],S[2],...
of lengths ranging from 1 to L
that are as large as possible and cover all N
elements of X
.
For example, given X = {5,6,7,100,100,7,8,5,4,4}
and L = 4
, the solution is S[1] = {5,6,7}, S[2] = {100, 100, 7, 8}, and S[3] = {5,4,4}
such that SUM[1] = 18, SUM[2] = 215, and SUM[3] = 13
. While the overall sum, no matter the subsets, will always be 246
, the key is that no other subsets with lengths ranging from 1 to L
will produce larger SUM[i]
, than those provided above.
Any help is greatly appreciated.