Divide an Array Into Subarrays With Minimum Cost I
Real-world version: Cutting the Conveyor Into Three StretchesThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def minimum_cost(weights: list[int]) -> int:public int minimumCost(int[] weights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 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
- Finding 3-Digit Even NumbersReal-world version: Three-Digit Even Numbers From the TilesEasy
- Removing Minimum Number of Magic BeansReal-world version: Levelling the Seed SacksMedium
- Merge Sorted ArrayReal-world version: Merge Into the Padded LogEasy
- Squares of a Sorted ArrayReal-world version: Sorted Drift EnergiesEasy