-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1000. Minimum Cost to Merge Stones.cpp
53 lines (47 loc) · 1.95 KB
/
1000. Minimum Cost to Merge Stones.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
int mergeStones(vector<int>& stones, int K) {
int n = stones.size();
if(n == 0) return -1;
if((n-1) % (K-1) != 0) return -1;
vector<int> psum(n+1);
psum[0] = 0;
for(int i = 0; i < n; i++){
psum[i+1] = psum[i] + stones[i];
}
vector<vector<vector<int>>> dp(n,
vector<vector<int>>(n,
vector<int>(K+1, -1)));
for(int i = 0; i < n; i++){
dp[i][i][1] = 0;
}
for(int len = 2; len <= n; len++){
for(int i = 0, j; i <= n-len; i++){
j = i + len - 1;
for(int m = 2; m <= K; m++){
if(len < m or (len - m) % (K-1) != 0) continue;
for(int k = i; k < j; k++){
int left_len = k - i + 1;
int right_len = len - left_len;
if((left_len - 1) % (K-1) != 0){
assert(dp[i][k][1] == -1);
continue;
}
if(right_len < m-1 or (right_len - (m-1)) % (K-1) != 0){
assert(dp[k+1][j][m-1] == -1);
continue;
}
if(dp[i][j][m] == -1) dp[i][j][m] = dp[i][k][1] + dp[k+1][j][m-1];
else dp[i][j][m] = min(dp[i][j][m], dp[i][k][1] + dp[k+1][j][m-1]);
}
}
// Compute dp[i][j][1]
if((len - 1) % (K-1) == 0){
assert(dp[i][j][K] != -1);
dp[i][j][1] = dp[i][j][K] + psum[j+1] - psum[i];
}
}
}
return dp[0][n-1][1];
}
};