Minimum Cost to Cut a Stick - Problem

Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:

[0] --- [1] --- [2] --- [3] --- [4] --- [5] --- [6]

Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.

You should perform the cuts in order, you can change the order of the cuts as you wish.

The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut).

Return the minimum total cost of the cuts.

Input & Output

Example 1 — Basic Case
$ Input: n = 7, cuts = [1,3,4,5]
Output: 16
💡 Note: One optimal order: cut at 5 (cost=7), then cut at 3 (cost=5), then cut at 4 (cost=2), then cut at 1 (cost=3). Total: 7+5+2+3=17. Better order exists with cost 16.
Example 2 — Single Cut
$ Input: n = 9, cuts = [5]
Output: 9
💡 Note: Only one cut at position 5, so the cost is the length of the entire stick: 9.
Example 3 — Two Cuts
$ Input: n = 6, cuts = [2,4]
Output: 8
💡 Note: Cut at 2 first (cost=6), then cut at 4 in right piece (cost=4-2=2). Total: 6+2=8. Or cut at 4 first (cost=6), then at 2 in left piece (cost=4-0=4). Total: 6+4=10. Minimum is 8.

Constraints

  • 2 ≤ n ≤ 106
  • 1 ≤ cuts.length ≤ min(n - 1, 100)
  • 1 ≤ cuts[i] ≤ n - 1
  • All the integers in cuts array are distinct.

Visualization

Tap to expand
Minimum Cost to Cut a Stick: Different Orders, Different CostsOriginal Stick: Length 7013457Bad Order: 1→3→4→5Cost: 7 + 6 + 4 + 2 = 19Good Order: 3→5→1→4Cost: 7 + 4 + 3 + 2 = 16Key Insight: Order of cuts affects total cost!Need to find optimal order using dynamic programmingMinimum Cost: 16
Understanding the Visualization
1
Input
Stick of length 7 with cuts at [1,3,4,5]
2
Process
Find optimal order of cuts to minimize total cost
3
Output
Minimum total cost is 16
Key Takeaway
🎯 Key Insight: The order of cuts matters - cutting creates smaller pieces which reduces future cutting costs
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
125.0K Views
Medium Frequency
~35 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen