-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
32 lines (32 loc) · 1.14 KB
/
Solution.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
class Solution {
public:
int palindromePartition(string s, int k) {
int len = s.length();
// cost[i][j] = min #changes needed to turn (s[i],...,s[i+j]) into a palindrome
vector<vector<int>> cost(len, vector<int>(len));
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
int begin = i, end = j, cnt = 0;
while (begin < end) {
if (s[begin] != s[end])
cnt++;
begin++, end--;
}
cost[i][j - i] = cnt;
}
}
// dp[i][j] = min #changes needed to split (s[i],...,s[len]) into j+1 palindromes
vector<vector<int>> dp(len, vector<int>(k, INT_MAX));
for (int i = 0; i < len; i++) {
dp[i][0] = cost[i][len - 1 - i];
}
for (int kk = 1; kk < k; kk++) {
for (int i = 0; i < len; i++) {
for (int j = i + 1; j + kk - 1 < len; j++) {
dp[i][kk] = min(dp[i][kk], dp[j][kk - 1] + cost[i][j - i - 1]);
}
}
}
return dp[0][k - 1];
}
};