All problems
0046HardArrayBinary SearchDynamic ProgrammingGreedyPrefix Sum

Balanced Truckloads

Tracked in this browser only
Write code

Trains the technique from

LeetCode 410Split Array Largest Sum

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_heaviest_load(weights: list[int], trucks: int) -> int:
Java
public int minHeaviestLoad(int[] weights, int trucks)
September 7
Apply