-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3445.cpp
More file actions
36 lines (35 loc) · 1.24 KB
/
3445.cpp
File metadata and controls
36 lines (35 loc) · 1.24 KB
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
class Solution {
public:
int toKey(int a, int b) {
return ((a & 1) << 1) | (b & 1);
}
int maxDifference(string s, int k) {
int res = INT_MIN;
int n = s.size();
for (char a = '0'; a <= '4'; ++a) {
for (char b = '0'; b <= '4'; ++b) {
if (a == b) continue;
int currA = 0, currB = 0;
int prevA = 0, prevB = 0;
vector<int> cache(4, INT_MAX);
int left = -1;
for (int right = 0; right < n; ++right) {
currA += (s[right] == a);
currB += (s[right] == b);
while (right - left >= k && currB - prevB >= 2) {
cache[toKey(prevA, prevB)] = min(cache[toKey(prevA, prevB)], prevA - prevB);
left++;
prevA += (s[left] == a);
prevB += (s[left] == b);
}
int status = toKey(currA, currB);
int match = cache[status ^ 0b10];
if (match != INT_MAX) {
res = max(res, currA - currB - match);
}
}
}
}
return res;
}
};