You get an integer array nums and an integer k. Cut nums into exactly k non-empty pieces, where each piece is a run of neighbouring elements and every element lands in exactly one piece. Each piece has a sum. Make the cuts so that the largest of those sums is as small as possible, and return that largest sum.
Cutting as [4,9] [3,8] [2,6] gives sums 13, 11 and 8. No cut does better: to keep every piece at 12 or under, the first piece can only be [4] and the second [9,3], which leaves [8,2,6] with a sum of 16.
With as many pieces as elements, each number stands alone, so the answer is the largest number.
One piece holds the whole array.
Constraints
1 <= nums.length <= 10000 <= nums[i] <= 10⁶1 <= k <= min(50, nums.length)Turn the question around: for a given limit L, can nums be cut into at most k pieces whose sums all stay at or under L? Going left to right and starting a new piece only when the next number would push the current one over L uses as few pieces as possible.
Choosing the cuts directly is hard. With n = 1000 and k = 50 there are C(999, 49) ways to place the 49 cuts, a number with 84 digits. Trying them all is out of the question, and a cut that helps one piece often hurts the next, so there's no obvious way to pick them one at a time.
Time O(n log S)Space O(1)
The full walkthrough and code are part of Premium
13