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的更多相关文章

  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. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  3. [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 ...

  4. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

  5. Split Array Largest Sum LT410

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  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. cdn提供商

    七牛,又拍 http://www.qiniu.com/ https://www.upyun.com/index.html

  2. localdb下载地址

    https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=29062 只能使用IE下载 http://www.microsoft.co ...

  3. yii2 如何用命名空间方式使用第三方类库

    原文地址:http://www.yiichina.com/tutorial/395 Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利 ...

  4. Orchard源码分析(7.2):Controller相关

    概述 默认情况下,ASP.NET MVC内置的DefaultControllerFactory负责Controller实例的创建.Orchard定义了一个继承自DefaultControllerFac ...

  5. Yii2框架与MongoDB拓展、Redis拓展的安装流程

    @author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...

  6. 说说C#的async和await(转)

    一个简单的例子: public class MyClass { public MyClass() { DisplayValue(); //这里不会阻塞 System.Diagnostics.Debug ...

  7. 关于JS的几点TIPS

    作为前端基本工作每天都会用到JS...但是我们对JS真的都了解吗,或者说有什么tips是我们不知道的呢.. So..此文关于JS的几点tips..... 一:定时器(可传多个参数) 首先是一个一般的定 ...

  8. HTTP状态301、404、200、304分别表示什么意思

    301 (永久移动)请求的网页已永久移动到新位置.服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置.您应使用此代码告诉 Googlebot 某个网页或网站已永久移动 ...

  9. tyvj1614 魔塔魔塔!

    描述 百度noip贴吧管理组开发了一个小游戏,叫魔塔魔塔.虽然把魔塔重复了两次,但其实还只是个魔塔而已,还是简化版的.游戏在一个N*M大小的地图中进行,每一格都是正方形.对于某一格,有若干种可能的状态 ...

  10. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...