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的情况下 ...
随机推荐
- 1.3.3、CDH 搭建Hadoop在安装之前(端口---CDH组件使用的端口)
列出的所有端口都是TCP. 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外部通信. Compone ...
- Light Explorer
[Light Explorer] The Light Explorer allows you to select and edit light sources. Window> Lighting ...
- 初探Mybaties整合分页插件PageHelper(1)
Mybaites整合分页PageHelper插件 在数据进行分页,通过sql语句,mysql分页,table_name表名,pageNum 第几页,pageSize 每页数据条数: SELECT * ...
- django 导入数据库
python manage .py makemigrations appname python manage.py migrate
- Spring mvc接收中文参数值乱码(tomcat配置问题)
问题| 使用java写的接口,中文参数乱码 问题分析| 请求方打印参数日志,中文无问题,tomcat中日志显示接收的参数乱码 实际是tomcat配置问题 解决方法| 在tomcat的配置文 ...
- hibernate ORA-17059 无法转换为内部表示
参考 https://jingyan.baidu.com/article/2fb0ba40a25a2b00f2ec5fc7.html 数据库里的字段类型与Java实体类中的对应字段属性类型不匹配
- centos 7.2 安装域名服务器(bind9.9 集群--主从架构),私有域名服务器+缓存
1.安装组件 yum install bind bind-utils -y 2.启动域名服务 service named start chkconfig named on ss -unlt |grep ...
- c# 文件过大时清空原有内容重新写入
FileStream fs = new FileStream("E:\\Test\\HistoryData.txt", FileMode.Append, FileAccess.Wr ...
- nth-child & nth-of-type区别
Do l have to 非做不可吗? He is my age. 他和我同岁. Here you are. 给你. No one knows . 没有人知道. 关于nth-child &&a ...
- FortiGate恢复出厂
1.需求 当需要把设备的所有配置删除,可进行恢复出厂操作.恢复出厂后所有配置都将丢失,若一定要恢复出厂设置建议前先备份当前的配置:备份配置操作步骤请参见"系统管理">> ...