-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3445.maximum-difference-between-even-and-odd-frequency-ii.cpp
More file actions
73 lines (65 loc) · 2.87 KB
/
3445.maximum-difference-between-even-and-odd-frequency-ii.cpp
File metadata and controls
73 lines (65 loc) · 2.87 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
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
#
# @lc app=leetcode id=3445 lang=cpp
#
# [3445] Maximum Difference Between Even and Odd Frequency II
#
# @lc code=start
class Solution {
public:
int maxDifference(string s, int k) {
int n = s.size();
int result = INT_MIN;
// Try all pairs (a, b) where a has odd freq and b has non-zero even freq
for (char a = '0'; a <= '4'; a++) {
for (char b = '0'; b <= '4'; b++) {
if (a == b) continue;
// Compute prefix sums
vector<int> pref_a(n + 1, 0), pref_b(n + 1, 0);
for (int i = 0; i < n; i++) {
pref_a[i + 1] = pref_a[i] + (s[i] == a ? 1 : 0);
pref_b[i + 1] = pref_b[i] + (s[i] == b ? 1 : 0);
}
// lists[pa][pb] stores (prefix_b_val, min_diff_up_to_here)
vector<vector<vector<pair<int, int>>>> lists(2, vector<vector<pair<int, int>>>(2));
for (int i = k; i <= n; i++) {
// Release j = i - k
int j = i - k;
int dj = pref_a[j] - pref_b[j];
int pa_j = pref_a[j] % 2;
int pb_j = pref_b[j] % 2;
int v_j = pref_b[j];
auto& lst = lists[pa_j][pb_j];
if (lst.empty()) {
lst.push_back({v_j, dj});
} else if (v_j > lst.back().first) {
lst.push_back({v_j, min(lst.back().second, dj)});
} else {
lst.back().second = min(lst.back().second, dj);
}
// Query at i
int di = pref_a[i] - pref_b[i];
int pa_i = pref_a[i] % 2;
int pb_i = pref_b[i] % 2;
int target_pa = 1 - pa_i;
int target_pb = pb_i;
int threshold = pref_b[i] - 2;
if (threshold >= 0) {
auto& target_lst = lists[target_pa][target_pb];
if (!target_lst.empty()) {
// Find the largest index where first <= threshold
auto it = upper_bound(target_lst.begin(), target_lst.end(), threshold,
[](int val, const pair<int, int>& p) { return val < p.first; });
if (it != target_lst.begin()) {
--it;
int min_j_diff = it->second;
result = max(result, di - min_j_diff);
}
}
}
}
}
}
return result;
}
};
# @lc code=end