Split Array Largest Sum
Hard
Topics
Given an array nums and an integer target (= k), split nums into target non-empty contiguous subarrays so as to minimize the largest subarray sum. Return that minimized largest sum.
Example 1
Input: nums = [7,2,5,10,8], target = 2 Output: 18
Example 2
Input: nums = [1,2,3,4,5], target = 2 Output: 9
Example 3
Input: nums = [1,4,4], target = 3 Output: 4
Constraints
- 1 <= nums.length <= 1000
- 0 <= nums[i] <= 10^6
- 1 <= target <= nums.length
Editorial — intuition, approach, complexity and pitfalls
Intuition
Minimising a maximum under a count constraint is the signature of binary search on the answer. Guess a cap on the largest allowed subarray sum and ask the much easier question: how few pieces does that cap permit?
Counting is a single greedy pass. Keep adding to the current piece while it fits under the cap; when the next element would overflow, start a new piece. That greedy is optimal for the piece count, because deliberately cutting early can never reduce the number of pieces needed later.
The search bounds matter. The cap can never be below max(nums) — that element has to live somewhere — and never needs to exceed sum(nums), which is one piece. Starting at max(nums) rather than 0 also keeps the greedy count well defined.
Approach
- Set
lo = max(nums)andhi = sum(nums). - For the midpoint cap, greedily count the pieces required.
- If the count fits within
target, the cap is feasible, so movehidown to it; otherwise moveloabove it. - Return the converged bound.
Dry Run
nums = [7,2,5,10,8] target = 2
lo = max = 10, hi = sum = 32
lo hi mid pieces needed fits in target
10 32 21 2 yes -- try a smaller cap
10 21 15 3 no -- cap too small
16 21 18 2 yes
16 18 17 3 no
answer 18 the split is [7,2,5] and [10,8], sums 14 and 18Complexity
- Time — O(n log(sum)): about 30 iterations at the stated bounds, each a linear greedy pass.
- Space — O(1).
- The interval DP over (index, pieces remaining) is O(n²·target) and is the other standard answer. It is strictly worse here and worth naming only to contrast.
Common Pitfalls
- Starting
loat 0 or 1. Any cap belowmax(nums)is infeasible, and the greedy counter can loop forever when a single element never fits. - Starting
hiatmax(nums) * targetor similar.sum(nums)is the tight bound and anything larger just wastes iterations. - Writing the greedy as "start a new piece when the running sum reaches the cap" rather than when it would exceed it. An element that exactly fills the cap belongs in the current piece.
- Forgetting to reset the running sum to 0 before adding the element that triggered the split. The shown code sets
cur = 0and then addsx, which is the ordering that works. - Counting pieces from 0. There is always at least one piece, so the counter starts at 1.
- Mixing the
lo <= hiandhi = mididioms, which does not terminate.
Full Solution — all languages
class Solution:
def splitArray(self, nums, target):
def need(cap):
c, cur = 1, 0
for x in nums:
if cur + x > cap:
c += 1
cur = 0
cur += x
return c
lo, hi = max(nums), sum(nums)
while lo < hi:
mid = (lo + hi) // 2
if need(mid) <= target:
hi = mid
else:
lo = mid + 1
return loimport java.util.*;
class Solution {
public int splitArray(int[] nums, int target) {
int lo = 0, hi = 0;
for (int x : nums) {
lo = Math.max(lo, x);
hi += x;
}
while (lo < hi) {
int mid = (lo + hi) / 2;
if (need(nums, mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
int need(int[] nums, int cap) {
int c = 1, cur = 0;
for (int x : nums) {
if (cur + x > cap) {
c++;
cur = 0;
}
cur += x;
}
return c;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int splitArray(vector<int>& nums, int target) {
int lo = 0, hi = 0;
for (int x : nums) {
lo = max(lo, x);
hi += x;
}
auto need = [&](int cap) {
int c = 1, cur = 0;
for (int x : nums) {
if (cur + x > cap) {
c++;
cur = 0;
}
cur += x;
}
return c;
};
while (lo < hi) {
int mid = (lo + hi) / 2;
if (need(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var splitArray = function(nums, target) {
let lo = 0, hi = 0;
for (const x of nums) {
lo = Math.max(lo, x);
hi += x;
}
const need = (cap) => {
let c = 1, cur = 0;
for (const x of nums) {
if (cur + x > cap) {
c++;
cur = 0;
}
cur += x;
}
return c;
};
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (need(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
};