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. putty加了密钥ssh不能登陆,PuTTY:server refused our key问题的解决(转)

    直接上方法:禁用系统的selinux功能,命令#setenforce0,但重启系统,selinux仍然启用.根治方法:更改SElinux的配置文件/etc/selinux/config,修改SELIN ...

  2. 安卓GreenDao框架一些进阶用法整理(转)

    大致分为以下几个方面: 一些查询指令整理 使用SQL语句进行特殊查询 检测表字段是否存在 数据库升级 数据库表字段赋初始值 一.查询指令整理 1.链式执行的指令 return mDaoSession. ...

  3. maven命令的简单理解

    mvn clean //在target文件夹中的一切都将被删除 mvn compile //编译源代码 mvn test  //运行应用程序中的单元测试 mvn package  //把jar打到本项 ...

  4. Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)

    Devexpress Gridview 提供了简单的求和,平均等方法,复杂的汇总方法则需要自定义,使用gridview 的CustomSummaryCalculate 事件,根据官网的文档及各论坛案例 ...

  5. 微信小程序开发——导航失效的解决办法

    异常描述: 使用 navigator 导航,各种属性配置没问题,就是点击死活不跳转. 异常分析: 遇到这种情况,要先考虑的就是当前配置的导航url,是不是已经使用在tabBar中,因为小程序对于nav ...

  6. PAT L2-011 玩转二叉树(二叉树层序遍历)

    给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出 ...

  7. linux命令学习之:vim

    1. 关于Vim vim是我最喜欢的编辑器,也是linux下第二强大的编辑器. 虽然emacs是公认的世界第一,我认为使用emacs并没有使用vi进行编辑来得高效. 如果是初学vi,运行一下vimtu ...

  8. php解析优酷网上的视频资源去广告

    1.过程原理解析: 一.准备工作 所谓工欲善其事必先利其器,做好破解的准备工作会令你事半功倍. 1.首先准备一个Http抓包工具,PC上推荐Fiddler或者Postman,iOS上推荐Surge 2 ...

  9. Head First Servlets & JSP 学习笔记 第一章 —— 前言和体系结构

    URL,Uniform Resource Locatiors,统一资源定位符. http:// www.wickedlysmart.com :80 /beeradivice/select /beer1 ...

  10. python sockerserver tcp 文件下载 udp

    #tcp serverclass MyHandler(socketserver.BaseRequestHandler): def handle(self): # 通信循环 while True: tr ...