File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(n^3)
2
+ // Space: O(n^2)
3
+
4
+ class Solution {
5
+ public:
6
+ int minCost (int n, vector<int >& cuts) {
7
+ vector<int > sorted_cuts (cbegin (cuts), cend (cuts));
8
+ sorted_cuts.emplace_back (0 );
9
+ sorted_cuts.emplace_back (n);
10
+ sort (begin (sorted_cuts), end (sorted_cuts));
11
+
12
+ vector<vector<int >> dp (sorted_cuts.size (), vector<int >(sorted_cuts.size ()));
13
+ for (int l = 2 ; l < sorted_cuts.size (); ++l) {
14
+ for (int i = 0 ; i + l < sorted_cuts.size (); ++i) {
15
+ dp[i][i + l] = numeric_limits<int >::max ();
16
+ for (int j = i + 1 ; j < i + l; ++j) {
17
+ dp[i][i + l] = min (dp[i][i + l], dp[i][j] + dp[j][i + l] +
18
+ sorted_cuts[i + l] - sorted_cuts[i]);
19
+ }
20
+ }
21
+ }
22
+ return dp[0 ][sorted_cuts.size () - 1 ];
23
+ }
24
+ };
You can’t perform that action at this time.
0 commit comments