-
Notifications
You must be signed in to change notification settings - Fork 5
/
BS_Longest1sAfterOneFlip.cpp
76 lines (63 loc) · 1.41 KB
/
BS_Longest1sAfterOneFlip.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
https://binarysearch.com/problems/Longest-1s-After-One-Flip
Longest 1s After One Flip
*/
int solve(string s) {
int n = s.length();
int i=0;
int j=0;
int ans=0;
int cntz=0;
while(j<s.size())
{
if(s[j] == '0')
cntz++;
while(i<j and cntz>=2)
{
if(s[i] == '0') cntz--;
i++;
}
ans = max(ans, j-i+1);
j++;
}
return ans;
}
int solve_1(string s) {
int ans = 0;
int ones = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
ans = max(ans, ++ones);
} else {
int curr = ones + 1;
for (int j = i + 1; j < s.size(); j++) {
if (s[j] == '1') {
curr++;
} else {
break;
}
}
ans = max(ans, curr);
ones = 0;
}
}
return ans;
}
int solve_2(string a) {
int n = a.size();
a = "0" + a + "0";
vector<int> l(n + 2, 0), r(n + 2, 0);
for (int i = 1; i <= n; i++)
if (a[i] == '1')
l[i] = l[i - 1] + 1;
else
l[i] = 0;
for (int i = n; i >= 1; i--)
if (a[i] == '1')
r[i] = r[i + 1] + 1;
else
r[i] = 0;
int ans = l[n];
for (int i = 1; i <= n; i++) ans = max(ans, l[i - 1] + r[i + 1] + 1);
return ans;
}