Minimum Cost to Cut a Stick

Pattern: Partition and interval DP

hard

Each cut costs the length of the piece being cut. Add both ends to the sorted cut positions; dp[i][j] is the cheapest way to make every cut between c[i] and c[j], choosing which of them is made first.

The problem

A stick of length n must be cut at every position in cuts, in any order. Each cut costs the length of the piece it cuts. Return the least total cost.

Think first

  • Why does order matter?
  • What does the first cut in a segment cost?
  • What does it leave?

Hints

    Brute force, and why it fails

    Try every order: m!.

    The key insight

    After the first cut in a segment, the two sides never affect each other.

    Dry run

    n = 7, cuts [1, 3, 4, 5]. Positions 0 1 3 4 5 7.

    Step 1 of 2

    The plan

    c = sorted(cuts + [0, n])
    dp[i][j] = c[j] - c[i] + min over k in (i, j) of dp[i][k] + dp[k][j]
    

    Complexity

    Time
    O(m³)
    Space
    O(m²)

    m is the number of cuts.

    Edge cases

    • One cut: n.
    • Cuts already sorted or not.
    • Adjacent cut positions.
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        public int minCost(int n, int[] cuts) {
            int m = cuts.length + 2;
            int[] c = new int[m];                   // the cut positions plus both ends, sorted
            for (int i = 0; i < cuts.length; i++) c[i + 1] = cuts[i];
            c[m - 1] = n;
            Arrays.sort(c);
            int[][] dp = new int[m][m];             // dp[i][j]: the cheapest way to make every cut strictly between c[i] and c[j]
            for (int len = 2; len < m; len++)
                for (int i = 0; i + len < m; i++) {
                    int j = i + len;
                    dp[i][j] = Integer.MAX_VALUE;
                    for (int k = i + 1; k < j; k++) // the first cut here is at c[k], on a piece of length c[j] - c[i]
                        dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + c[j] - c[i]);
                }
            return dp[0][m - 1];
        }
    }

    C++

    class Solution {
    public:
        int minCost(int n, vector<int>& cuts) {
            vector<int> c(cuts);                    // the cut positions plus both ends, sorted
            c.push_back(0);
            c.push_back(n);
            sort(c.begin(), c.end());
            int m = static_cast<int>(c.size());
            vector<vector<int>> dp(m, vector<int>(m, 0));   // the cheapest way to make every cut strictly between c[i] and c[j]
            for (int len = 2; len < m; len++)
                for (int i = 0; i + len < m; i++) {
                    int j = i + len;
                    dp[i][j] = INT_MAX;
                    for (int k = i + 1; k < j; k++) // the first cut here is at c[k], on a piece of length c[j] - c[i]
                        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + c[j] - c[i]);
                }
            return dp[0][m - 1];
        }
    };

    Python

    class Solution:
        def minCost(self, n: int, cuts: list[int]) -> int:
            c = sorted(cuts + [0, n])               # the cut positions plus both ends
            m = len(c)
            dp = [[0] * m for _ in range(m)]        # the cheapest way to make every cut strictly between c[i] and c[j]
            for length in range(2, m):
                for i in range(m - length):
                    j = i + length                  # the first cut here is at c[k], on a piece of length c[j] - c[i]
                    dp[i][j] = min(dp[i][k] + dp[k][j] for k in range(i + 1, j)) + c[j] - c[i]
            return dp[0][m - 1]

    Common mistakes

    • Using n instead of the piece length.
    • Forgetting to sort.

    Similar problems