Split Array Largest Sum
Real-world version: Balanced TruckloadsThe statement here is our own: the same inputs, outputs and technique.
Parcels are queued along a loading bay in one fixed line, and their masses are the integer array weights. A fleet of trucks vehicles is waiting at the far end.
The crew works front to back and never reorders the queue, so each vehicle is handed one unbroken run of neighbouring parcels. Every parcel is loaded, no run overlaps another, and no vehicle may drive off empty. A vehicle's load is the total mass of the run it received.
The shift only ends when the most heavily laden vehicle has finished its route, so the crew wants the biggest of the trucks loads to come out as small as it possibly can. Report the mass of that heaviest load under the best possible cut.
Examples
Example 1
- Input
- weights = [4, 9, 3, 12, 6, 1], trucks = 3
- Output
- 15
Cutting the line into 4 9 | 3 12 | 6 1 gives loads of 13, 15 and 7, and no other set of two cuts pushes the biggest load below 15.
Example 2
- Input
- weights = [2, 3, 4, 5], trucks = 4
- Output
- 5
There are exactly as many vehicles as parcels, so each vehicle carries a single parcel and the heaviest load is the heaviest parcel.
Example 3
- Input
- weights = [8, 1, 1, 1], trucks = 1
- Output
- 11
With one vehicle the whole queue travels together, so the answer is the total mass.
Constraints
- 1 <= weights.length <= 1000
- 0 <= weights[i] <= 10^6
- 1 <= trucks <= min(50, weights.length)
- Each run of parcels must be contiguous and the queue order is fixed
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def min_heaviest_load(weights: list[int], trucks: int) -> int:public int minHeaviestLoad(int[] weights, int trucks)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Subsequence With Limited SumReal-world version: Parcels Within Each AllowanceEasy
- Frequency of the Most Frequent ElementReal-world version: Plates at One ThicknessMedium
- Maximum Coins From K Consecutive BagsReal-world version: Richest Run of Consecutive BinsMedium
- Maximum Frequency After Subarray OperationReal-world version: Most Dials Reading k After One NudgeMedium