Split Array Largest Sum
Split Array Largest Sum is LeetCode 410 (Hard). You get an integer array nums and an integer k. Cut nums into exactly k non-empty pieces, each one a contiguous subarray, and look at the biggest piece sum. Return the smallest value that biggest sum can have.
- Numbers are between 0 and 10⁶, and there are at most 1,000 of them.
kis at most 50 and never more thannums.length, so every input has a valid split and there is no "impossible" answer to return.
The total is at most 10⁹, which fits in a 32-bit int.
- 1 <= nums.length <= 1000
- 0 <= nums[i] <= 10⁶
- 1 <= k <= min(50, nums.length)
Intuition
Choosing where the k - 1 cuts go is hard. Testing a fixed limit is easy. Say no piece may sum to more than cap. Walk left to right, keep adding numbers to the current piece, and cut only when the next number would push it past cap. That gives the fewest pieces any split can have under cap.
- One direction: a bigger
capnever needs more pieces, so the limits that need at mostkpieces form one unbroken run. - Where to look: the answer is at least
max(nums), since some piece holds the largest number, and at mostsum(nums), a single piece.
Binary search that range for the smallest limit that works.
"Cut into k contiguous parts and make the worst part as small as possible" is a minimise-the-maximum question. Guess the maximum, count how many parts that guess forces, and binary search the guess.
Approach
Before reading on, pick a limit such as 20 for [7, 2, 5, 10, 8] and count how many pieces it forces. Then ask how that count changes as the limit grows. Aim for O(n log S), where S is the sum of the array.
Two ways to solve it
Guess the largest piece sum, count the pieces it forces, and halve the range.
- Fast: about 30 counting passes.
- Needs: numbers that are not negative, so a piece's sum only grows as it gets longer.
dp[j][i] is the best biggest sum for the first i numbers cut into j pieces. Try every start for the last piece.
- Works with negatives: it never relies on sums growing.
- Slow in Python at full size without the early break.
Binary search wins: about 30 passes over at most 1,000 numbers. The DP is the idea many people reach first and is worth knowing, but it does up to k·n² = 5·10⁷ steps. The steps, code and live demo follow binary search; the DP code comes after the demo.
Set the search range
Set lo = max(nums) and hi = sum(nums). A number cannot be split across pieces, so no limit below the largest one can work, and the full sum always works because one piece can take everything.
Count the pieces a limit forces
Start with one piece and a running sum of 0. For each number, if adding it would pass cap, start a new piece holding just that number; otherwise add it. Cutting only when forced keeps every piece as long as possible, so the count is the smallest one possible.
Halve the range
While lo < hi, try mid = (lo + hi) // 2. If it needs at most k pieces, set hi = mid, because mid works and something smaller might too. Otherwise set lo = mid + 1, because mid and every limit below it need too many pieces.
Why fewer than k pieces still counts as a yes
A limit that needs only 2 pieces when k = 3 is still a valid answer: cut any piece again and no sum grows. The promise k <= nums.length is what makes this safe: there are always enough numbers to make the extra cuts.
Return the meeting point
When lo == hi, that value works and every smaller value was ruled out, so return it.
Code in Python, C++ and Java
DP over prefixes
prev[p] is the best biggest sum when the first p numbers are cut into j - 1 pieces. For the first i numbers in j pieces, the last piece is nums[p:i], so the cost of that choice is max(prev[p], sum of nums[p:i]). Walking p from right to left makes the last piece grow, so once it alone reaches the best value found, no smaller p can help and the loop stops.
Common pitfalls
Starting the search below the largest number
lo, hi = 0, sum(nums)
lo, hi = max(nums), sum(nums)
When a number is bigger than cap, the count still opens a piece for it and carries on, so a limit that no split can meet gets reported as fine. On [3, 9] with k = 2 the search returns 3, but the piece holding 9 sums to 9.
Testing for exactly k pieces
if pieces_needed(mid) == k:
hi = midif pieces_needed(mid) <= k:
hi = mid== k rejects every limit that needs fewer pieces, even though such limits work, so the search drifts upward. On [1, 4, 4] with k = 3, limits 4 to 8 need only 2 or 3 pieces, and the search returns 9 instead of 4.
Counting pieces from 0
pieces, run = 0, 0
pieces, run = 1, 0
The first piece exists before any cut is made. Starting at 0 reports one piece fewer than the truth, so limits that need k + 1 pieces pass. On [7, 2, 5, 10, 8] with k = 2 the search returns 14 instead of 18.
Edge cases
max(nums) and sum(nums) are both 0, so the loop never runs and the answer is 0. Zeros elsewhere are harmless: adding 0 never pushes a piece past the limit.
Each number sits alone, so the answer is max(nums). Every limit from there up needs at most k pieces, so the search closes on lo.
Complexity
nums and S is sum(nums) - max(nums). S is at most 10⁹, so the loop runs about 30 times, each with one O(n) counting pass. Only a few integers are stored.Split Array Largest Sum FAQ
Is this the same as Allocate Minimum Pages?
The task is the same: contiguous groups, smallest possible largest group. Two details differ. Allocate Minimum Pages allows more students than books and expects -1 then, while here k <= nums.length is promised, so there is always an answer. Its inputs also reach 10⁶ books of up to 10⁴ pages, so totals need 64-bit integers; here the total stays within 10⁹.
Why is the answer always the sum of some actual piece?
Let C be the value the search returns and run the greedy cut with limit C. Its largest piece sum M is at most C, and those same cuts prove that M works. C is the smallest value that works, so M cannot be below it: M = C. For [7, 2, 5, 10, 8] the answer 18 is the piece [10, 8].
What if the array could contain negative numbers?
Binary search on the limit breaks. Adding a number can now shrink a piece's sum, so cutting only when forced is no longer the best plan, and max(nums) is no longer a lower bound. The DP version still works, because it tries every start for the last piece instead of relying on sums only growing.