Skip to content

Create 3445. Maximum Difference Between Even and Odd Frequency II #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions 3445. Maximum Difference Between Even and Odd Frequency II
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class MinBIT {
public:
static const int MAX;
int n;
vector<int> data;

MinBIT(int length) {
n = length;
data.assign(length + 2, MAX);
}

void insert(int index, int value) {
for (int i = index + 1; i <= n; i += i & -i) {
data[i] = min(data[i], value);
}
}

int getMin(int index) {
int result = MAX;
for (int i = index + 1; i > 0; i -= i & -i) {
result = min(result, data[i]);
}
return result;
}
};

const int MinBIT::MAX = INT_MAX;

class Solution {
public:
int maxDifference(string s, int k) {
int length = s.size();
int result = INT_MIN;

for (int first = 0; first < 5; ++first) {
for (int second = 0; second < 5; ++second) {
if (first == second) continue;

vector<int> diff(length + 1, 0);
vector<int> parityA(length + 1, 0);
vector<int> parityB(length + 1, 0);
vector<int> countB(length + 1, 0);

for (int i = 1; i <= length; ++i) {
int digit = s[i - 1] - '0';
diff[i] = diff[i - 1] + (digit == first ? 1 : 0) - (digit == second ? 1 : 0);
parityA[i] = (parityA[i - 1] + (digit == first ? 1 : 0)) & 1;
parityB[i] = (parityB[i - 1] + (digit == second ? 1 : 0)) & 1;
countB[i] = countB[i - 1] + (digit == second ? 1 : 0);
}

MinBIT storage[2][2] = {
{ MinBIT(length + 1), MinBIT(length + 1) },
{ MinBIT(length + 1), MinBIT(length + 1) }
};

for (int j = 0; j <= length; ++j) {
if (j >= k) {
int back = j - k;
int pA = parityA[back];
int pB = parityB[back];
int bCount = countB[back];
storage[pA][pB].insert(bCount, diff[back]);
}

if (j > 0 && countB[j] > 0) {
int altA = 1 - parityA[j];
int curB = parityB[j];
int minPrev = storage[altA][curB].getMin(countB[j] - 1);

if (minPrev != MinBIT::MAX) {
result = max(result, diff[j] - minPrev);
}
}
}
}
}

return result == INT_MIN ? 0 : result;
}
};
Loading