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. osharpV3数据库初始化

    var databaseInitializer = new DatabaseInitializer(); databaseInitializer.MapperAssemblyFinder = new ...

  2. Python之路【第五篇续】:面向对象编程二

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABgQAAALaCAIAAABxja8cAAAgAElEQVR4nOzd6X9Tdd74/+uv+f5uzF

  3. [c#]params可变参数

    摘要 在项目中多多少少会用到params这个关键字,来修饰参数,它的作用,让该参数的个数是可变的,并且可变参数必须是方法的最后一个参数.但如何判断到底有没有为该参数传递值,怎么判断? 一个例子 sta ...

  4. scp命令的用法详解

    这篇文章主要是参考了http://blog.csdn.net/jiangkai_nju/article/details/7338177这个博客,要看详细的内容可以参考这个博客进行学习研究,但是我觉得在 ...

  5. Sublime Text 2 快捷键 (windows)

    转自:http://istyles.blog.163.com/blog/static/1811003892011828111418654/ Lucifr翻译了 Sublime Text 2 快捷键 M ...

  6. HighCharts日期及数值格式化

    1.函数原型   1 dateFormat(Stringformat,[Numbertime],[Booleancapitalize])::String 2.说明 格式化JavaScript 时间(也 ...

  7. Highcharts X轴名称太长,如何设置下面这种样式

      Highcharts所有的图表除了饼图都有X轴和Y轴,默认情况下,x轴显示在图表的底部,y轴显示在左侧(多个y轴时可以是显示在左右两侧),通过chart.inverted = true 可以让x, ...

  8. WebService 之 WSDL文件 讲解

    原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...

  9. gflags

    一.安装配置 下载地址: https://code.google.com/p/gflags/downloads/list 解压安装: tar zxvf gflags-2.0.tar.gz && ...

  10. CLGeocoder "Lost connection to geod" #error# when use geocodeAddressString:completionHandler

      I got this warning when I tried to get destination using CLGeoCoder and the warning is coming out ...