Split Array Largest Sum(二分,dp?)

Given an array which consists of non-negative integers and an integerm, you can split the array intomnon-empty continuous subarrays. Write an algorithm to minimize the largest sum among thesemsubarrays.

Note: If n is the length of array, assume the following constraints are satisfied:

1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)

Examples:

Input:
nums = [7,2,5,10,8]
m = 2

Output:
18

Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

分析:

看成找一个target,使得分成的m个subarray的sum都<=target,所以用二分,这个target在max(nums)和sum(nums)之间。在这个区间左右移动算是已排序数组。

辅助函数中cnt是切下去的刀数。刀数+1 = group数目

这里二分不知道边界怎么做了

dp:前面j组和i结尾,然后j,i之间开始切分最后一组。有点类似背包最后一个value开分。

Python可以 python3不行

Last updated

Was this helpful?