Advertisement
1988coder

410. Split Array Largest Sum

Feb 5th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/split-array-largest-sum/
  2.  
  3. /**
  4.  * Binary Search
  5.  *
  6.  * Refer:
  7.  * https://leetcode.com/problems/split-array-largest-sum/discuss/89817/Clear-Explanation:-8ms-Binary-Search-Java
  8.  *
  9.  * The answer is between maximum value of input array numbers and sum of those
  10.  * numbers.
  11.  *
  12.  * Use binary search to approach the correct answer. We have l = max number of
  13.  * array; r = sum of all numbers in the array; Every time we do mid = (l + r) /
  14.  * 2;
  15.  *
  16.  * Use greedy to narrow down left and right boundaries in binary search.
  17.  *
  18.  * 1. Cut the array from left.
  19.  *
  20.  * 2. Try our best to make sure that the sum of numbers between each two cuts
  21.  * (inclusive) is large enough but still less than mid.
  22.  *
  23.  * 3. We'll end up with two results: either we can divide the array into more
  24.  * than m subarrays or we cannot.
  25.  *
  26.  * If we can, it means that the mid value we pick is too small because we've
  27.  * already tried our best to make sure each part holds as many non-negative
  28.  * numbers as we can but we still have numbers left. So, it is impossible to cut
  29.  * the array into m parts and make sure each parts is no larger than mid. We
  30.  * should increase m. This leads to l = mid + 1;
  31.  *
  32.  * If we can't, it is either we successfully divide the array into m parts and
  33.  * the sum of each part is less than mid, or we used up all numbers before we
  34.  * reach m. Both of them mean that we should lower mid because we need to find
  35.  * the minimum one. This leads to r = mid;
  36.  *
  37.  * Time Complexity: O(N + N * log(S)) = O(N * log S)
  38.  *
  39.  * Space Complexity: O(1)
  40.  *
  41.  * N = Length of input array. S = Sum of all numbers in the array.
  42.  */
  43. class Solution {
  44.     public int splitArray(int[] nums, int m) {
  45.         if (nums == null || nums.length == 0 || m <= 0 || nums.length < m) {
  46.             return 0;
  47.         }
  48.  
  49.         int max = 0;
  50.         int sum = 0;
  51.         for (int n : nums) {
  52.             max = Math.max(max, n);
  53.             sum += n;
  54.         }
  55.  
  56.         if (m == nums.length) {
  57.             return max;
  58.         }
  59.         if (m == 1) {
  60.             return sum;
  61.         }
  62.  
  63.         int left = max;
  64.         int right = sum;
  65.  
  66.         while (left < right) {
  67.             int mid = left + (right - left) / 2;
  68.             if (isValid(nums, m, mid)) {
  69.                 right = mid;
  70.             } else {
  71.                 left = mid + 1;
  72.             }
  73.         }
  74.  
  75.         return left;
  76.     }
  77.  
  78.     private boolean isValid(int[] nums, int m, int sum) {
  79.         int curSum = 0;
  80.         int setCount = 1;
  81.  
  82.         for (int n : nums) {
  83.             if (curSum + n > sum) {
  84.                 setCount++;
  85.                 if (setCount > m) {
  86.                     return false;
  87.                 }
  88.                 curSum = n;
  89.             } else {
  90.                 curSum += n;
  91.             }
  92.         }
  93.  
  94.         return true;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement