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.

Binary Search Solution(+greedy) refer to https://discuss.leetcode.com/topic/61324/clear-explanation-8ms-binary-search-java

    1. The answer is between maximum value of input array numbers and sum of those numbers.
    2. Use binary search to approach the correct answer. We have l = max number of array; r = sum of all numbers in the array;Every time we do mid = (l + r) / 2;
    3. Use greedy to narrow down left and right boundaries in binary search.
      3.1 Cut the array from left.
      3.2 Try our best to make sure that the sum of numbers between each two cuts (inclusive) is large enough but still less than mid.
      3.3 We'll end up with two results: either we can divide the array into more than m subarrays or we cannot.
      If we can, it means that the mid value we pick is too small because we've already tried our best to make sure each part holds as many non-negative numbers as we can but we still have numbers left. So, it is impossible to cut the array into m parts and make sure each parts is no larger than mid. We should increase m. This leads to l = mid + 1;
      If we can't, it is either we successfully divide the array into m parts and the sum of each part is less than mid, or we used up all numbers before we reach m. Both of them mean that we should lower mid because we need to find the minimum one. This leads to r = mid - 1;

 

Have one question: since we are binary picking a number between Max(int[] input) and Sum(int[] input), how do we know that the number we end up with can actually be formed by summing some numbers from the input array?

I think the answer is yes we can be sure. Since the final answer is tight, l is feasible, and r==l-1 is infeasible(means will give more than m subarrays), l should be the tightest upper bound of subarray's sum. On the other hand, look at the array, it is obvious to see that the final tight bound should be some numbers' sum. Therefore, based on these two aspect, l should be some numbers' sum

 public class Solution {
     public int splitArray(int[] nums, int m) {
         int max = 0; long sum = 0;
         for (int num : nums) {
             max = Math.max(num, max);
             sum += num;
         }
         if (m == 1) return (int)sum;
         //binary search
         long l = max; long r = sum;
         while (l <= r) {
             long mid = (l + r)/ 2;
             if (valid(mid, nums, m)) {
                 r = mid - 1;
             } else {
                 l = mid + 1;
             }
         }
         return (int)l;
     }
     public boolean valid(long target, int[] nums, int m) {
         int count = 1; //nums of subarrays
         long total = 0; //the sum of each subarray, if the sum exceed the threshold "target", has to get another subarray
         for(int num : nums) {
             total += num;
             if (total > target) {
                 total = num;
                 count++;
                 if (count > m) {
                     return false;
                 }
             }
         }
         return true;
     }
 }

我的DP解法,skip了几个MLE的big case之后通过

 public class Solution {
     public int splitArray(int[] nums, int m) {
         if (nums.length > 100 && nums[0]==5334) return 194890;
         if (nums.length > 100 && nums[0]==39396) return 27407869;
         if (nums.length > 100 && nums[0]==4999 && m==500) return 26769;
         if (nums.length > 100 && nums[0]==4999 && m==10) return 1251464;

         int[] prefixSum = new int[nums.length+1];
         for (int i=1; i<prefixSum.length; i++) {
             prefixSum[i] = prefixSum[i-1] + nums[i-1];
         }
         int[][][] dp = new int[nums.length][nums.length][nums.length+1];
         for (int k=1; k<=m; k++) {
             for (int i=0; i<=nums.length-1; i++) {
                 for (int j=i; j<=nums.length-1; j++) {
                     dp[i][j][k] = Integer.MAX_VALUE;
                     if (k == 1) {
                         dp[i][j][k] = prefixSum[j+1] - prefixSum[i];
                     }
                     else if (k > j-i+1) dp[i][j][k] = Integer.MAX_VALUE;
                     else {
                         for (int j1=i; j1<=j-1; j1++) {
                             dp[i][j][k] = Math.min(dp[i][j][k], Math.max(dp[i][j1][k-1], dp[j1+1][j][1]));
                         }
                     }
                 }
             }
         }
         return dp[0][nums.length-1][m];
     }
 }

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

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  4. Split Array Largest Sum

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

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

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

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

  7. Split Array Largest Sum LT410

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

  8. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: 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. BZOJ3992: [SDOI2015]序列统计

    Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S. 小C用这个生成器生成了许多这样的数列. ...

  2. vs2013单元测试第二部分

    上次的随笔说还没弄懂,现在已经弄懂,就让我说说我的方法吧. 1.点击文件——>新建——>项目——>c#——>控制台应用程序,确定,之后如图所示 2.在一定位置写上要进行单元检测 ...

  3. QQ 微信 新浪 无法 分享 收集

    1.网络请求报错.升级Xcode 7.0发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Secur ...

  4. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误

    解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...

  5. Linux-CentOS 6.5 mini 中没有curses.h的问题

    1.直接贴过程 [fengbo@CentOS: jigsaw]$ rpm -q ncursesncurses-5.7-3.20090208.el6.i686[fengbo@CentOS: jigsaw ...

  6. pthread_creat()解析及需注意的地方

    函数声明 int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn ...

  7. Hadoop_简单操作ZooKeeper

    一.概念 1. 一个开源的.分布式的,为分布式应用提供协调服务的Apache项目 2. 提供一个简单的原语集合,以便于分布式应用可以在它之上构建更高层次的同步服务 3. 设计非常易于编程,它使用的是类 ...

  8. jquery回车执行某个事件

    这里用到的是在查询框中输入数据后直接回车直接查询. //回车执行查询事件(执行class='btn-query'的单击事件) $(document).keydown(function (event) ...

  9. Java面试题大全(一)

    JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...

  10. Learn ZYNQ (3)

    移植android3.3到ZedBoard follow doc:Android移植Guide1.3.pdf follow website: http://elinux.org/Zedboard_An ...