15. Minimum Cost to Cut a Stick of length N
β GFG solution to the Minimum Cost to Cut a Stick problem: find minimum total cost to perform all cuts on a wooden stick using interval dynamic programming technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a wooden stick of length n, labeled from 0 to n. You are also given an integer array cuts[], where each element cuts[i] represents a position along the stick at which you can make a cut.
Each cut costs an amount equal to the length of the stick being cut at that moment. After performing a cut, the stick is divided into two smaller sticks.
You can perform the cuts in any order. Your task is to determine the minimum total cost required to perform all the cuts.
π Examples
Example 1
Input: n = 10, cuts[] = [2, 4, 7]
Output: 20
Explanation: If we cut the stick in the order [4, 2, 7], the cost will be 10 + 4 + 6 = 20,
which is the minimum total cost.Example 2
Input: n = 8, cuts[] = [1, 6, 3, 5]
Output: 19
Explanation: If we cut the stick in the order [3, 6, 1, 5], the cost will be 8 + 5 + 3 + 3 = 19,
which is the minimum total cost.π Constraints
$2 \le n \le 10^6$
$1 \le \text{cuts}[i] \le n - 1$
$1 \le \text{cuts.size()} \le 100$
β
My Approach
The optimal approach uses Interval Dynamic Programming to solve this problem efficiently:
Interval DP with Bottom-Up Approach
Add Boundaries:
Add 0 and n to the cuts array to represent the stick's endpoints.
Sort the cuts array to process intervals in order.
Define DP State:
dp[i][j]represents the minimum cost to make all cuts between positioncuts[i]andcuts[j].The cost of cutting between positions
cuts[i]andcuts[j]iscuts[j] - cuts[i].
Build DP Table:
Iterate over all possible gap sizes (length of intervals).
For each interval
[i, j], try all possible cut positionskbetween them.The cost for cutting at position
kis:dp[i][k] + dp[k][j] + cuts[j] - cuts[i].Choose the minimum cost among all possible cut positions.
Base Case:
When there are no cuts between two positions (
j - i <= 1), the cost is 0.
Result:
The answer is stored in
dp[0][sz-1], representing the entire stick from 0 to n.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(mΒ³), where m is the total number of cuts including boundaries (m = cuts.size() + 2). We have O(mΒ²) subproblems and each subproblem requires O(m) time to try all possible cut positions.
Expected Auxiliary Space Complexity: O(mΒ²), as we use a 2D DP table to store the minimum cost for all possible intervals between cuts.
π§βπ» Code (C++)
class Solution {
public:
int minCutCost(int n, vector<int>& cuts) {
cuts.push_back(0);
cuts.push_back(n);
sort(cuts.begin(), cuts.end());
int sz = cuts.size();
vector<vector<int>> dp(sz, vector<int>(sz, 0));
for (int gap = 2; gap < sz; gap++) {
for (int i = 0; i + gap < sz; i++) {
int j = i + gap;
dp[i][j] = INT_MAX;
for (int k = i + 1; k < j; k++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + cuts[j] - cuts[i]);
}
}
}
return dp[0][sz - 1];
}
};β Code (Java)
class Solution {
public int minCutCost(int n, int[] cuts) {
int m = cuts.length + 2;
int[] c = new int[m];
c[0] = 0;
c[m - 1] = n;
System.arraycopy(cuts, 0, c, 1, cuts.length);
Arrays.sort(c);
int[][] dp = new int[m][m];
for (int gap = 2; gap < m; gap++) {
for (int i = 0; i + gap < m; i++) {
int j = i + gap;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i + 1; k < j; k++) {
dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + c[j] - c[i]);
}
}
}
return dp[0][m - 1];
}
}π Code (Python)
class Solution:
def minCutCost(self, n, cuts):
cuts = [0] + sorted(cuts) + [n]
m = len(cuts)
dp = [[0] * m for _ in range(m)]
for gap in range(2, m):
for i in range(m - gap):
j = i + gap
dp[i][j] = float('inf')
for k in range(i + 1, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + cuts[j] - cuts[i])
return dp[0][m - 1]π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated