forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-changes-to-make-k-semi-palindromes.cpp
145 lines (139 loc) · 5.53 KB
/
minimum-changes-to-make-k-semi-palindromes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Time: O(n * nlogn + n^3 + n^2 * k) = O(n^3)
// Space: O(n * nlogn) = O(n^2 * logn)
// number theory, dp
class Solution {
public:
int minimumChanges(string s, int k) {
vector<vector<int>> divisors(size(s) + 1);
for (int i = 1; i < size(divisors); ++i) { // Time: O(nlogn), Space: O(nlogn)
for (int j = i; j < size(divisors); j += i) {
divisors[j].emplace_back(i);
}
}
vector<vector<unordered_map<int, int>>> dp(size(s), vector<unordered_map<int, int>>(size(s)));
for (int l = 1; l <= size(s); ++l) { // Time: O(n * nlogn + n^3), Space: O(n * nlogn)
for (int left = 0; left + l - 1 < size(s); ++left) {
const int right = left + l - 1;
for (const auto& d : divisors[l]) {
int c = 0;
for (int i = 0; i < d; ++i) {
if (s[left + i] != s[right - (d - 1) + i]) {
++c;
}
}
dp[left][right][d] = (left + d < right - d ? dp[left + d][right - d][d] : 0) + c;
}
}
}
vector<vector<int>> dp2(size(s), vector<int>(size(s), size(s)));
for (int i = 0; i < size(s); ++i) { // Time: O(n^2 * logn + n^2 * k), Space: O(n * k)
for (int j = i + 1; j < size(s); ++j) {
int c = size(s);
for (const auto& d : divisors[j - i + 1]) {
if (d != j - i + 1) {
c = min(c, dp[i][j][d]);
}
}
dp2[i][j] = c;
}
}
vector<int> dp3(size(s) + 1, size(s));
dp3[0] = 0;
for (int l = 0; l < k; ++l) { // Time: O(n^2 * logn + n^2 * k), Space: O(n * k)
vector<int> new_dp3(size(s) + 1, size(s));
for (int i = 0; i < size(s); ++i) {
for (int j = l * 2; j < i; ++j) { // optimized for the fact that the length of semi-palindrome is at least 2
new_dp3[i + 1] = min(new_dp3[i + 1], dp3[j] + dp2[j][i]);
}
}
dp3 = move(new_dp3);
}
return dp3[size(s)];
}
};
// Time: O(n * nlogn + n^3 + n^2 * k) = O(n^3)
// Space: O(n * nlogn) = O(n^2 * logn)
// number theory, dp
class Solution2 {
public:
int minimumChanges(string s, int k) {
vector<vector<int>> divisors(size(s) + 1);
for (int i = 1; i < size(divisors); ++i) { // Time: O(nlogn), Space: O(nlogn)
for (int j = i; j < size(divisors); j += i) {
divisors[j].emplace_back(i);
}
}
vector<vector<unordered_map<int, int>>> dp(size(s), vector<unordered_map<int, int>>(size(s)));
for (int l = 1; l <= size(s); ++l) { // Time: O(n * nlogn + n^3), Space: O(n * nlogn)
for (int left = 0; left + l - 1 < size(s); ++left) {
const int right = left + l - 1;
for (const auto& d : divisors[l]) {
int c = 0;
for (int i = 0; i < d; ++i) {
if (s[left + i] != s[right - (d - 1) + i]) {
++c;
}
}
dp[left][right][d] = (left + d < right - d ? dp[left + d][right - d][d] : 0) + c;
}
}
}
vector<vector<int>> dp2(size(s) + 1, vector<int>(k + 1, size(s)));
dp2[0][0] = 0;
for (int i = 0; i < size(s); ++i) { // Time: O(n^2 * logn + n^2 * k), Space: O(n * k)
for (int j = 0; j < i; ++j) {
int c = size(s);
for (const auto& d : divisors[i - j + 1]) {
if (d != i - j + 1) {
c = min(c, dp[j][i][d]);
}
}
for (int l = 0; l < k; ++l) {
dp2[i + 1][l + 1] = min(dp2[i + 1][l + 1], dp2[j][l] + c);
}
}
}
return dp2[size(s)][k];
}
};
// Time: O(n^2 * nlogn + n^2 * k) = O(n^3 * logn)
// Space: O(nlogn + n * k)
// number theory, dp
class Solution3 {
public:
int minimumChanges(string s, int k) {
vector<vector<int>> divisors(size(s) + 1);
for (int i = 1; i < size(divisors); ++i) { // Time: O(nlogn), Space: O(nlogn)
for (int j = i + i; j < size(divisors); j += i) {
divisors[j].emplace_back(i);
}
}
const auto& dist = [&](int left, int right, int d) {
int result = 0;
for (int i = 0; i < (right - left + 1) / 2; ++i) {
if (s[left + i] != s[right - ((i / d + 1) * d - 1) + (i % d)]) {
++result;
}
}
return result;
};
const auto& min_dist = [&](int left, int right) { // Time: O(nlogn)
int result = size(s);
for (const auto& d : divisors[right - left + 1]) {
result = min(result, dist(left, right, d));
}
return result;
};
vector<vector<int>> dp(size(s) + 1, vector<int>(k + 1, size(s)));
dp[0][0] = 0;
for (int i = 0; i < size(s); ++i) { // Time: O(n^2 * nlogn + n^2 * k), Space: O(n * k)
for (int j = 0; j < i; ++j) {
const int c = min_dist(j, i);
for (int l = 0; l < k; ++l) {
dp[i + 1][l + 1] = min(dp[i + 1][l + 1], dp[j][l] + c);
}
}
}
return dp[size(s)][k];
}
};