使用动态规划得到每种分割方式得到的最小的数组最大值
class Solution {
public:
int splitArray(vector& nums, int m) {
int n = nums.size();
vector sum(n + 1, 0);
for (int i = 1; i <= n; i ++) {
sum[i] = sum[i - 1] + nums[i - 1];
}
vector<vector> dp(n + 1, vector(m + 1, 2e18));
for (int j = 0; j <= m; j ++) dp[0][j] = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= min(m, i); j ++) {
for (int k = j; k <= i; k ++) {
dp[i][j] = min(dp[i][j], max(dp[k-1][j - 1], sum[i] - sum[k-1]));
}
}
}
return dp[n][m];
}
};