Formatted question description: https://leetcode.ca/all/1547.html
1547. Minimum Cost to Cut a Stick (Hard)
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:
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). Please refer to the first example for a better explanation.
Return the minimum total cost of the cuts.
Example 1:
Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
Example 2:
Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
Constraints:
2 <= n <= 10^6
1 <= cuts.length <= min(n  1, 100)
1 <= cuts[i] <= n  1
 All the integers in
cuts
array are distinct.
Related Topics:
Dynamic Programming
Solution 1. DP
To simplify the code, we insert 0
and n
into the A
.
Let dp[i][j]
be the minimum cost if we cut on the stick from A[i]
to A[j]
.
 If
j == i + 1
(len == 2
), we can’t cut on this stick, sodp[i][j] = 0
.  If
j == i + 2
(len == 3
), the only choice you have is to cut in the middle, and the cost is the length of this stick, sodp[i][j] = A[i + 2]  A[i]
.  If
j > i + 2
(len >= 4
), we can try to cut atk
wherei < k < j
and use the minimum cost we can get which is the cost of cutting the left partdp[i][k]
plus the cost of cutting the right partdp[k][j]
and the cost of the current cutA[j]  A[i]
, sodp[i][j] = min( dp[i][k] + dp[k][j] + A[j]  A[i]  i < k < j)
The answer is dp[0][N1]
.
// OJ: https://leetcode.com/problems/minimumcosttocutastick/
// Time: O(N^3)
// Space: O(N^2)
class Solution {
public:
int minCost(int n, vector<int>& A) {
A.push_back(0);
A.push_back(n);
sort(begin(A), end(A));
int N = A.size(), inf = INT_MAX;
vector<vector<int>> dp(N, vector<int>(N, inf));
for (int i = 0; i + 1 < N; ++i) dp[i][i + 1] = 0;
for (int i = 0; i + 2 < N; ++i) dp[i][i + 2] = A[i + 2]  A[i];
for (int i = N  4; i >= 0; i) {
for (int j = i + 3; j < N; ++j) {
int val = inf;
for (int k = i + 1; k < j; ++k) val = min(val, dp[i][k] + dp[k][j]);
dp[i][j] = val + A[j]  A[i];
}
}
return dp[0][N  1];
}
};
Solution 2. Knuth’s Optimization
// OJ: https://leetcode.com/problems/minimumcosttocutastick/
// Time: O(N^2)
// Space: O(N^2)
// Ref: https://leetcode.com/problems/minimumcosttocutastick/discuss/804270/O(N2)DP(Knuth'sOptimization)
class Solution {
public:
int minCost(int n, vector<int>& A) {
A.push_back(0);
A.push_back(n);
sort(begin(A), end(A));
int N = A.size();
vector<vector<int>> dp(N, vector<int>(N)), mid(N, vector<int>(N));
for (int len = 0; len < N; ++len) {
for (int i = 0; i + len < N; ++i) {
int j = i + len;
if (len < 2) {
mid[i][j] = i;
continue;
}
dp[i][j] = A[j]  A[i];
int val = INT_MAX;
for (int k = mid[i][j  1]; k <= mid[i + 1][j]; ++k) {
int next = dp[i][k] + dp[k][j];
if (next < val) {
val = next;
mid[i][j] = k;
}
}
dp[i][j] += val;
}
}
return dp[0][N  1];
}
};
Java

class Solution { Map<Long, Integer> map = new HashMap<Long, Integer>(); static final int INFINITY = 100000000; public int minCost(int n, int[] cuts) { Arrays.sort(cuts); int length = cuts.length; map.put((long) cuts[0], 0); for (int i = 1; i < length; i++) { long key = (long) cuts[i  1] * (long) (n + 1) + cuts[i]; map.put(key, 0); } map.put((long) (cuts[length  1]) * (long) (n + 1) + n, 0); return calculate(n, 0, n, cuts); } public int calculate(int n, int start, int end, int[] cuts) { long key = (long) start * (long) (n + 1) + end; if (map.containsKey(key)) return map.get(key); int minCost = INFINITY; int length = cuts.length; for (int i = 0; i < length; i++) { int cut = cuts[i]; if (cut > start && cut < end) { int cost = (end  start) + calculate(n, start, cut, cuts) + calculate(n, cut, end, cuts); minCost = Math.min(minCost, cost); } else if (cut >= end) break; } map.put(key, minCost); return minCost; } }

// OJ: https://leetcode.com/problems/minimumcosttocutastick/ // Time: O(N^3) // Space: O(N^2) class Solution { public: int minCost(int n, vector<int>& A) { A.push_back(0); A.push_back(n); sort(begin(A), end(A)); int N = A.size(), inf = INT_MAX; vector<vector<int>> dp(N, vector<int>(N, inf)); for (int i = 0; i + 1 < N; ++i) dp[i][i + 1] = 0; for (int i = 0; i + 2 < N; ++i) dp[i][i + 2] = A[i + 2]  A[i]; for (int i = N  4; i >= 0; i) { for (int j = i + 3; j < N; ++j) { int val = inf; for (int k = i + 1; k < j; ++k) val = min(val, dp[i][k] + dp[k][j]); dp[i][j] = val + A[j]  A[i]; } } return dp[0][N  1]; } };

print("Todo!")