A wooden stick of length n is marked at positions 0 to n. The array cuts lists the positions where you must cut. You may perform the cuts in any order. Each cut costs the length of the stick piece being cut, and splits it into two smaller pieces. Return the minimum total cost of performing all the cuts.
A wooden stick of length n is marked at positions 0 to n. The array cuts lists the positions where you must cut. You may perform the cuts in any order. Each cut costs the length of the stick piece being cut, and splits it into two smaller pieces. Return the minimum total cost of performing all the cuts.
Example 1:
Input: {"n":7,"cuts":[1,3,4,5]}
Output: 16
Constraints:
2≤n≤106
1≤cuts.length≤min(n−1,100)
1≤cuts[i]≤n−1
All the integers in cuts are distinct.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"n":7,"cuts":[1,3,4,5]}
Sort the cuts and add the two stick ends: points = [0, 1, 3, 4, 5, 7]. dp[i][j] will be the cheapest way to make every cut strictly between points[i] and points[j].
stick (points spaced evenly, labels are real positions)
0p01p13p24p35p47p5
dp[i][j] = min cost for all cuts between p_i and p_j