All problems
1152EasyArraySortingEnumeration

Cutting the Conveyor Into Three Stretches

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3010Divide an Array Into Subarrays With Minimum Cost I

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.

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.

Examples

Example 1

Input
weights = [1, 2, 3, 4, 5]
Output
6

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

Input
weights = [5, 4, 3, 2, 1]
Output
8

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

Input
weights = [1, 1, 1]
Output
3

Every crate weighs 1 and three stretches have to be charged.

Constraints

  • 3 <= weights.length <= 50
  • 1 <= weights[i] <= 50

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 minimum_cost(weights: list[int]) -> int:
Java
public int minimumCost(int[] weights)
September 7
Apply