-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumCosttoCutaStick.cpp
More file actions
27 lines (26 loc) · 929 Bytes
/
minimumCosttoCutaStick.cpp
File metadata and controls
27 lines (26 loc) · 929 Bytes
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
// Source: https://leetcode.com/problems/minimum-cost-to-cut-a-stick/
// Author: Miao Zhang
// Date: 2021-05-15
/******************************************************************
* dp[i][j]: dp[i][k] + dp[k][j] + cut[j] - cut[i]
* dp[0][n]
******************************************************************/
class Solution {
public:
int minCost(int n, vector<int>& cuts) {
sort(begin(cuts), end(cuts));
cuts.insert(cuts.begin(), 0);
cuts.push_back(n);
int m = cuts.size();
vector<vector<int>> dp(m, vector<int>(m));
for (int i = m - 1; i >= 0; i--) {
for (int j = i + 1; j < m; j++) {
dp[i][j] = ((i + 1) == j ? 0 : 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][m - 1];
}
};