Trains the technique from
LeetCode 3010Divide an Array Into Subarrays With Minimum Cost IThis 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.
A conveyor holds crates whose weights are weights. Cut it into exactly three stretches of neighbouring crates, each stretch holding at least one crate.
A stretch is charged the weight of its own first crate, and the total charge is the three added up.
Return the smallest total charge.
Example 1
The first stretch has to begin at the first crate, so its 1 is unavoidable. The cheapest two crates left to begin stretches at are the 2 and the 3.
Example 2
The 5 is forced. Beginning the other two stretches at the 1 and the 2 gives 8, and no other pair is cheaper.
Example 3
Every crate weighs 1 and three stretches have to be charged.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def minimum_cost(weights: list[int]) -> int:public int minimumCost(int[] weights)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.