Given an integer array nums and an integer k, split nums into
k non-empty contiguous subarrays such that the largest sum of any subarray
is minimized. Return the minimized largest sum of the split.
nums = [7,2,5,10,8], k = 218nums = [1,2,3,4,5], k = 29nums = [1,4,4], k = 34The feasibility function is monotone: if we can split nums into ≤ k subarrays with max sum ≤ M, then we can certainly do the same with max sum ≤ M+1. This monotone property lets us binary search for the minimum M that is still feasible. S = sum(nums) is the size of the search space.
Greedy check: Scan left→right, accumulate current sum. When the next element would push us over mid, start a new partition. Count partitions needed.
Trying every way to place the cuts grows very fast. Dynamic programming works, but binary search is simpler and faster.
Guess a limit for the largest sum. Fill parts greedily, and count how many parts that limit needs. If it needs at most k parts, the limit works, so try smaller.
Minimize the largest part sum by binary searching that sum. This same template solves shipping, books and painters.
Run the animation above on your own input, or watch the 1:09 video explanation of Split Array Largest Sum with a dry run and Java code.