#1547

Minimum Cost to Cut a Stick

Hard Partition DP (MCM style)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A stick of length n must be cut at given positions, in any order. Each cut costs the length of the piece being cut. Return the minimum total cost.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the cheapest order of cuts, where each cut costs the current piece length.

Why this works: interval DP over the sorted cut positions: dp[i][j] = piece length + the best first cut k inside.

Example: n = 7, cuts [1, 3, 4, 5]

cutting at 3 first, then 5, then 1 and 4 → 16

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Building the DP over the stick length n (up to 10^6) instead of over the cut positions.

Solve on LeetCode ↗
Complexity
  • Time: O(m³), m = number of cuts.
  • Space: O(m²).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon