Split Array Largest Sum
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.
Note:
Given m satisfies the following constraint: 1 ≤ m ≤ length(nums) ≤ 14,000.
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. 分析:暴力解法 虽然要把数组分成几份,但是因为数组里的元素是连续的,所以,我们可以把前面部分当成第一份,然后找出后边部分分出 m - 1次时最大的和。所以递归方法可解。但是,很明显,cost太高了。
public class Solution {
//brute-force approach
public int splitArray(int[] nums, int m) {
long[] sums = new long[nums.length];
sums[] = nums[]; for (int i = ; i < sums.length; i++) {
sums[i] = sums[i - ] + nums[i];
} return (int) helper(nums, sums, , sums.length - , m);
} public long helper(int[] nums, long[] sums, int start, int end, int k) {
if (k <= )
return Integer.MAX_VALUE;
if (k == ) {
if (start == ) {
return sums[end];
} else {
return sums[end] - sums[start - ];
}
}
long min = Long.MAX_VALUE; for (int i = start; i <= end; i++) { long leftSum;
if (start == ) {
leftSum = sums[i];
} else {
leftSum = sums[i] - sums[start - ];
} min = Math.min(min, Math.max(leftSum, helper(nums, sums, i + , end, k - )));
}
return min;
}
}
第二种方法:
首先找出数组的最大值max和所有值的和sum。然后那个最小值一定是在max和sum之间。 所以可以利用binary search来寻找。
reference: https://discuss.leetcode.com/topic/61315/java-easy-binary-search-solution-8ms
public class Solution {
public int splitArray(int[] nums, int m) {
long sum = ;
int max = ;
for (int num : nums) {
max = Math.max(max, num);
sum += num;
}
return (int) binary(nums, m, sum, max);
} private long binary(int[] nums, int m, long high, long low) {
long mid = ;
while (low <= high) {
mid = (high + low) / ;
if (valid(nums, m, mid)) {
high = mid - ;
} else {
low = mid + ;
}
}
return low;
} private boolean valid(int[] nums, int m, long max) {
int cur = , count = ;
for (int num : nums) {
cur += num;
if (cur > max) {
cur = num;
count++;
if (count > m) {
return false;
}
}
}
return true;
}
}
Split Array Largest Sum的更多相关文章
- [LeetCode] 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 ...
- Split Array Largest Sum LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 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的情况下 ...
随机推荐
- VS2013快捷键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- Lua 之os库
标准os库 os.rename(oldname, newname) 文件重命名: os.remove(filename) 删除一个文件 os.execute(cmd) os.execute可运行一条系 ...
- apache struts 2 任意代码执行漏洞
漏洞检测地址:http://0day.websaas.cn 漏洞利用工具,如下: 漏洞利用,如下: step1 step2 step3 提权思路,如下: 1.开启虚拟终端,执行命令,但是,提示“连接被 ...
- php 利用ffmpeg将amr转MP3
原文地址: http://www.jianshu.com/p/895d5568ce70 http://www.cnblogs.com/wanghetao/p/3386311.html http://w ...
- Java多线程编程核心技术---线程间通信(二)
通过管道进行线程间通信:字节流 Java提供了各种各样的输入/输出流Stream可以很方便地对数据进行操作,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据,一个线程发送 ...
- rabbitmq的web管理界面无法使用guest用户登录
安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...
- CXF bus interceptor配置
作用:BUS是cxf的支架,它主要担当扩展及拦截器提供者的角色. 在这里主要讲讲 bus的interceptor的功能 目前配置cxf的interceptor主要有2中方法: 1.通过xml配置文件的 ...
- 使用 Ant 自动生成项目构建版本
引言 对 于多版本项目,要提供新版本来跟上新功能或缺陷报告增加的速度,并同时仍然保持可接受的质量水平,可能是一项不小的挑战.构建自动化可确保准确性和消除人 为错误的可能性,从而部分地解决此问题.自动化 ...
- linux lsmod命令 及相关信息
lsmod (list modules) 语法:lsmod 功能: lsmod命令:是一个小程序,用来显示文件.proc/modules的信息,也就是显示当前内核模块装载的模块. 补充说明: 执行l ...
- mybatis 基础1(动态代理)
我目前使用的是mybatis 3.3.0版本. 可使用 1.xml文本, 2.dao类, 3.sqlSession.getMapper(Class<T> type), 生成sql类, 原理 ...