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.

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.

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

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

Idea 1. dynmaic programming, similar to Capacity To Ship Packages Within D Days LT1011

dp(i)(m) = min(i <= k <= n - m) max(prefixSum(k+1) - prefixSum(i), dp(k+1)(m-1))

Note: prefixSum integer overflow, pls use long.

Time complexity: O(mn^2)

Space complexity: O(n)

 class Solution {
public int splitArray(int[] nums, int m) {
int n = nums.length; long[] prefixSum = new long[n+1];
for(int i = 1; i <= n; ++i) {
prefixSum[i] = prefixSum[i-1] + nums[i-1];
} long[] dp = new long[n];
for(int i = 0; i < n; ++i) {
dp[i] = prefixSum[n] - prefixSum[i];
} for(int split = 2; split <= m; ++split) {
for(int i = 0; i <= n -split; ++i) {
for(int k = i; k <= n - split; ++k) {
long val = Math.max(prefixSum[k+1] - prefixSum[i], dp[k+1]);
dp[i] = Math.min(dp[i], val);
// if(val <= dp[i]) { //positive optimisation
// dp[i] = val;
// }
// else {
// break;
// }
}
}
} return (int)dp[0];
}
}

Idea 2. binary search

search space: min = max(max(nums), sum(nums)/m), max = sum(nums)

 class Solution {
private int checkSplits(int[] nums, int load) {
int splits = 1;
int sum = 0;
for(int num: nums) {
if(sum + num > load) {
++splits;
sum = num;
}
else sum += num;
}
return splits;
}
public int splitArray(int[] nums, int m) {
int n = nums.length; long sum = 0;
int minSum = 0;
for(int num: nums) {
minSum = Math.max(minSum, num);
sum += num;
} int maxSum = (int)(sum);
minSum = Math.max(minSum, (int)(sum-1)/m + 1);
while(minSum < maxSum) {
int mid = minSum + (maxSum - minSum)/2;
int splits = checkSplits(nums, mid);
if(splits <= m) {
maxSum = mid;
}
else {
minSum = mid + 1;
}
}
return minSum;
}
}

Split Array Largest Sum LT410的更多相关文章

  1. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  2. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  3. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  4. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

  6. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  7. [LeetCode] 410. Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. 【leetcode】410. Split Array Largest Sum

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  9. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

随机推荐

  1. propTypes

    [propTypes] React.PropTypes is deprecated as of React v15.5. Please use the prop-types library inste ...

  2. Sublime Text使用中的一些心得

    Sublime Text3是每个web前端程序员的必备神器,其中有许多便利的功能及插件.下面列出一些在开发中比较实用的快捷操作,可以极大地提高代码的编写速度及效率. l  在文档中输入代码,即使忘记保 ...

  3. MVC003之调用BLL层方法(BLL层调用了WebService)

    项目又BLL类库,在类库中引用了webservice.在web层调用BLL的方法时 错误如下: 在 ServiceModel 客户端配置部分中,找不到引用协定“OAService.IntranetSe ...

  4. Errors running builder 'Faceted Project Validation Builder' on project

    右键eclipse中的工程,选择properties,选择build,去掉出问题的validation校验项,重启eclipse即可.

  5. oracle_效率优化

    1.并行和强制走索引的用法 SELECT/*+parallel(T 16) parallel(B 16) parallel(D 16)*/ T.POLICY_NO, T.DEPARTMENT_CODE ...

  6. JMeter学习(四)参数化(转载)

    转载自 http://www.cnblogs.com/yangxia-test JMeter也有像LR中的参数化,本篇就来介绍下JMeter的参数化如何去实现. 参数化:录制脚本中有登录操作,需要输入 ...

  7. python学习day6 for循环 字符串的内置方法

    1.for循环 和while相比 l=[1,2,3] i=0 while i <len(l) print(l[i]) i+=1 l=['a','b','c'] for item in l: pr ...

  8. es快照定时备份脚本

    #!/bin/bashdata1=`date "+%Y%m%d"`data2="http://0.0.0.0:9200/_snapshot/my_backup/snaps ...

  9. Monkey 命令收集相关 --追加Monkey自动化测试开源工具

    .1.环境配置 MONKEY测试使用的是ADB命令,因此只需要配置ADB环境即可. 2.测试准备与执行 在Monkey测试前,必须进行以下准备 Ø  手机屏幕超时设置为30分钟或者永不超时,防止手机进 ...

  10. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛----随手记录帖

    这是跟学长学姐组队来打的最爽的一次比赛了,也可能是互相组队最后一次比赛了,南哥和楼学姐,省赛之后就退役了,祝他们能考研和面试都有happy ending! 虽然最后没有把F题的n^2约数的数学题写完, ...