-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3499.cpp
More file actions
29 lines (28 loc) · 733 Bytes
/
Copy path3499.cpp
File metadata and controls
29 lines (28 loc) · 733 Bytes
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
class Solution {
public:
int maxActiveSectionsAfterTrade(string s) {
int curr = s[0] - '0';
int initial = curr;
vector<int> counts;
int totalActive = 0;
int cnt = 0;
for (auto& c : s) {
totalActive += c - '0';
if (c - '0' == curr) {
cnt++;
}
else {
counts.push_back(cnt);
cnt = 1;
curr ^= 1;
}
}
counts.push_back(cnt);
int res = totalActive;
int m = counts.size();
for (int i = initial; i + 2 < m; i += 2) {
res = max(res, counts[i] + totalActive + counts[i + 2]);
}
return res;
}
};