-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-beautiful-splits-in-an-array.cpp
61 lines (58 loc) · 1.89 KB
/
count-beautiful-splits-in-an-array.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
// Time: O(n^2)
// Space: O(n)
// z-function
class Solution {
public:
int beautifulSplits(vector<int>& nums) {
// Template: https://cp-algorithms.com/string/z-function.html
const auto& z_function = [](const auto& s, int left, int right) { // Time: O(n), Space: O(n)
vector<int> z(right - left + 1);
for (int i = 1, l = 0, r = 0; i < size(z); ++i) {
if (i <= r) {
z[i] = min(r - i + 1, z[i - l]);
}
while (i + z[i] < size(z) && s[left + z[i]] == s[left + i + z[i]]) {
++z[i];
}
if (i + z[i] - 1 > r) {
l = i, r = i + z[i] - 1;
}
}
return z;
};
int result = 0;
const auto& z0 = z_function(nums, 0, size(nums) - 1);
for (int i = 1; i + 1 < size(nums); ++i) {
const auto& zi = z_function(nums, i, size(nums) - 1);
for (int j = i + 1; j < size(nums); ++j) {
if ((z0[i] >= i && (j - i >= i)) || zi[j - i] >= j - i) {
++result;
}
}
}
return result;
}
};
// Time: O(n^2)
// Space: O(n^2)
// dp
class Solution2 {
public:
int beautifulSplits(vector<int>& nums) {
vector<vector<int>> dp(size(nums), vector<int>(nums));
for (int i = size(nums) - 1; i >= 0; --i) {
for (int j = i + 1; j < size(nums); ++j) {
dp[i][j] = (nums[i] == nums[j]) ? 1 + (j + 1 < size(nums) ? dp[i + 1][j + 1] : 0) : 0;
}
}
int result = 0;
for (int i = 1; i + 1 < size(nums); ++i) {
for (int j = i + 1; j < size(nums); ++j) {
if ((dp[0][i] >= i && (j - i >= i)) || dp[i][j] >= j - i) {
++result;
}
}
}
return result;
}
};