671. Minimum Cost to Cut a Stick

HardStackDynamic ProgrammingInterval DP

A stick of length n has marked positions given in cuts. Each cut costs the current length of the stick being cut; performing cuts in different orders yields different total costs. Return the minimum total cost to perform all cuts. The input is JSON {n, cuts}.

Input: JSON {n, cuts}.

Output: Integer — the minimum total cutting cost.

Examples

Example 1
Input: {"n":7,"cuts":[1,3,4,5]}
Output: 16
Explanation: An optimal order costs 16.
Example 2
Input: {"n":9,"cuts":[5,6,1,4,2]}
Output: 22
Explanation: Optimal ordering.
Example 3
Input: {"n":5,"cuts":[2]}
Output: 5
Explanation: A single cut costs the full length.

Constraints

Asked by

AmazonMicrosoftGoogleBloombergMeta
Solve this problem in the editor →