I have an array with 2 <= n <= 100
doubles:
A = [a1, a2, ... , an], ai > 0
and an integer 2 <= k <= min(n, 20)
. I need to split A
into k
subarrays:
B1 = [a1, a2, ... , ap]
B2 = [ap+1, ap+2, ... , aq]
...
Bk = [aw+1, aw+2, ... , an]
such that the sum in each B
is almost equal (it's hard to give a strict definition what this means - I'm interested in an approximate solution).
Example:
Input: A = [1, 2, 1, 2, 1], k=2
Output: [[1, 2, 1], [2, 1]] or [[1, 2], [1, 2, 1]]
I tried a probabilistic approach:
sample from
[1, 2, .., n]
usingA
as probability weightscut the sample into quantiles to find a good partition,
but this was not stable enough for production.
tl;dr This question asks about 2-chunk divisions. I need k
-chunk division.