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的情况下 ...
随机推荐
- wampserver 2.5安装pear win8.1
集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql 5.6.17 apache 2.4.9 ...
- json_decode
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ...
- Rsession让Java调用R更简单
Rsession让Java调用R更简单 R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒. ...
- 浙大PAT-1001
1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...
- 在nodejs下express 从安装到运行的全过程
安装过程: npm install -gd express npm install -g express-generator express -V //查看版本,现在一般都是4.x系列的 expr ...
- 包介绍 - Fluent Validation (用于验证)
Install-Package FluentValidation 如果你使用MVC5 可以使用下面的包 Install-Package FluentValidation.MVC5 例子: public ...
- 【转载】Unity 合理安排增量更新(热更新)
原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. --------------------------------- ...
- 【转载】利用Unity自带的合图切割功能将合图切割成子图
虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...
- 删除某一BSC在某一时间段内的数据
DELETE FROM cdl_raw_do_12501_ztev8_sht_201509 WHERE call_start_time >= STR_TO_DATE( '2015-09-14 2 ...
- [转]C++模板学习
1. 模板的概念. 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数 ...