← DiffPush

Minimum Cost to Cut a Stick

DiffPush Tier Dynamic Programming · DP on Partition O(m^3) · O(m^2)

The Marble Slab Cutting Bay

A stone workshop must slice a marble slab at a list of marked positions. Each pass of the saw bills the current length of the piece being cut, and the foreman may execute the marks in any order. Scheduling smartly — cutting expensive pieces apart early — can slash the invoice, so the shop wants the cheapest total billing plan.

Input: An integer n (stick length) and an array cuts of distinct cut positions.

Output: Return the minimum total cost to perform all cuts, where each cut costs the length of the piece cut.

Constraints

Examples

Example 1

Input: {"n":7,"cuts":[1,3,4,5]}
Output: 16
Cutting 1, then 5, then 3, then 4 keeps every billed length small — total 16.

Example 2

Input: {"n":9,"cuts":[5,6,1,4,2]}
Output: 22
Order 2, 1, 4, 5, 6 achieves the minimum billing of 22.

Solve this in your browser →

Also on LeetCode ↗