Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.
(分割数组的最大值最小)
Note:
If n is the length of array, assume the following constraints are satisfied:
- 1 ≤ n ≤ 1000
- 1 ≤ m ≤ min(50, n)
Example:
1. 二分查找
首先设置 left = max(nums), right = sum(nums)。然后我们在 [left, right] 区间去查找最小的 _sum ,根据 _sum 去划分数组使得划分的子数组的数目小于等于 m ,具体实现过程如下:
1 | class Solution: |
2. 动态规划
二维动规:
数组dp:dp[i][j]表示将数组中前i个数字分成j组所能得到的最小的各个子数组中最大值。
递推公式:dp[i][j] = min(dp[i][j], max(dp[k][j-1], sums[i] - sums[k])。
其中dp[k][j-1]表示数组中前k个数字分成j-1组所能得到的最小的各个子数组中最大值,而sums[i]-sums[k]就是后面的数字之和,我们取二者之间的较大值;然后和dp[i][j]原有值进行对比,更新dp[i][j]为二者之中的较小值;这样k在[0,i-1]的范围内扫过一遍,dp[i][j]就能更新到最小值。棘突实现过程如下:
(Java 会 AC,Python 会 TLE)
1 | class Solution: |