Split Array Largest Sum LT410
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的更多相关文章
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- [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 ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
随机推荐
- java 递归 树形
//菜单树形结构 public JSONArray treeMenuList(JSONArray menuList, int parentId) { JSONArray childMenu = new ...
- java中map接口hashMap以及Enty之间的用法和关系
java中map接口hashMap以及Enty之间的转换 首先说的是map接口: Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value ...
- 初探Mybaties整合分页插件PageHelper(1)
Mybaites整合分页PageHelper插件 在数据进行分页,通过sql语句,mysql分页,table_name表名,pageNum 第几页,pageSize 每页数据条数: SELECT * ...
- RECON-NG
web搜索框架,python开发,与msf命令形式相似. 创建独立的工作区 recon-ng -w sina 可以看到就转到了新建的工作区sina里 为搜索框架指定API key keys add A ...
- appium +ios 判断元素是否存在,排除visible=“false”的数据
问题 想要判断name=xxx的元素是否存在,存在的话进行点击,结果页面并没有展示我要的元素时也提示找到了元素 原因 ios通过driver.find_element_by_name(“name值 ...
- 可上下拖动且有浮沉动画的View
package com.ifenglian.superapp1; import android.animation.Animator;import android.animation.Animator ...
- Failed to start LSB: Bring up/down networking 问题
Failed to start LSB: Bring up/down networking 问题 1.执行 service network restart 出现以下错误 Restarting ne ...
- 第九章 词典 (d2)散列:排解冲突(2)
- 如何彻底卸载mysql(xp)
如何彻底卸载mysql 完整的卸载MySQL 5.x 的方法: 1.控制面板里的增加删除程序内进行删除 2.删除MySQL的安装文件夹C:\Program Files\MySQL,如果备份好,可以直接 ...
- Chrome格式化JavaScript代码
很多第三方插件的脚本,是压缩后的代码,甚至时动态加载的,代码只有一行. Chrome提供了格式化脚本代码的功能,方便加断点调试. 1 在Sources面板中,点击脚本名称,打开脚本源码. 2 点击左下 ...