Algorithm to split an array into P subarrays of balanced sum

前端 未结 10 1732
面向向阳花
面向向阳花 2020-12-08 05:00

I have an big array of length N, let\'s say something like:

2 4 6 7 6 3 3 3 4 3 4 4 4 3 3 1

I need to split this array into P subarrays (in

10条回答
  •  醉梦人生
    2020-12-08 05:47

    If I am not mistaken here, one more approach is dynamic programming.

    You can define P[ pos, n ] as the smallest possible "penalty" accumulated up to position pos if n subarrays were created. Obviously there is some position pos' such that

    P[pos', n-1] + penalty(pos', pos) = P[pos, n]

    You can just minimize over pos' = 1..pos.

    The naive implementation will run in O(N^2 * M), where N - size of the original array and M - number of divisions.

提交回复
热议问题