Split Array Largest Sum

Hard

Topics
ArrayBinary SearchGreedy

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

702152103|84
Split into 2 contiguous parts so the largest part is as small as possible. Best achievable: 18, with the cuts marked.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [7,2,5,10,8], target = 2
Output: 18

Example 2

10213243|54
Input:  nums = [1,2,3,4,5], target = 2
Output: 9

Example 3

1041|42|
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

  1. Set lo = max(nums) and hi = sum(nums).
  2. For the midpoint cap, greedily count the pieces required.
  3. If the count fits within target, the cap is feasible, so move hi down to it; otherwise move lo above it.
  4. 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 18

Complexity

  • 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 lo at 0 or 1. Any cap below max(nums) is infeasible, and the greedy counter can loop forever when a single element never fits.
  • Starting hi at max(nums) * target or 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 = 0 and then adds x, 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 <= hi and hi = mid idioms, 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 lo
Asked at Pinterest
SensAI
Beta
Tap the mic to stop voice mode