classSolution{ publicintsplitArray(int[] nums, int m){ int minSum = Integer.MIN_VALUE; int maxSum = 0; int len = nums.length; for (int i = 0; i < len; i++) { if (nums[i] > minSum) { minSum = nums[i]; } maxSum += nums[i]; }
int left = minSum; int right = maxSum + 1; while (left < right) { int mid = left + (right - left) / 2; int currentM = calculateM(nums, mid); if (currentM == m) { right = mid; } elseif (currentM > m) { left = mid + 1; } elseif (currentM < m) { right = mid; } } return left; }
publicintcalculateM(int[] nums, int sum){ int i = 0; int tmpSum = sum; int m = 0; while (i < nums.length) { if (tmpSum >= nums[i]) { tmpSum -= nums[i]; i++; if (i == nums.length) { m++; } } else { tmpSum = sum; m++; } } return m; } }