Split Array Largest Sum
Minimize the maximum subarray sum when splitting an array into k contiguous pieces using binary search on the answer with greedy validation.
01The problem
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.
Think of k workers sharing ordered tasks sequentially: how do you distribute the workload so that the most burdened worker has to do as little work as possible?
| Input | Output | Why |
|---|---|---|
nums = [7, 2, 5, 10, 8], k = 2 | 18 | Best split is [7, 2, 5] (sum 14) and [10, 8] (sum 18) |
nums = [1, 2, 3, 4, 5], k = 2 | 9 | Best split is [1, 2, 3] (sum 6) and [4, 5] (sum 9) |
nums = [1, 4, 4], k = 3 | 4 | Best split has one element per subarray, max sum is 4 |
Constraints: 1 to 1,000 numbers, values from 0 to 1,000,000, k between 1 and min(50, n).
Loading the rest of the lesson…