-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrod_cutting.cpp
29 lines (25 loc) · 944 Bytes
/
rod_cutting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
int minCost(int n, vector<int>& cuts) {
// Add the two ends of the rod to the cuts list
cuts.push_back(0);
cuts.push_back(n);
// Sort the cuts array
sort(cuts.begin(), cuts.end());
int m = cuts.size();
// Create a DP table to store the minimum costs
vector<vector<int>> dp(m, vector<int>(m, 0));
// Fill the DP table
for (int length = 2; length < m; ++length) {
for (int i = 0; i < m - length; ++i) {
int j = i + length;
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 the minimum cost to cut the rod from start to end
return dp[0][m - 1];
}
};