-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlip.cpp
More file actions
30 lines (30 loc) · 678 Bytes
/
Flip.cpp
File metadata and controls
30 lines (30 loc) · 678 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
29
30
vector<int> Solution::flip(string A) {
int n = A.size(),i;
vector <int> Vec(n,1);
int cnt = 0;
for(i=0;i<n;i++)
if (A[i]=='1'){
Vec[i] = -1;
cnt++;
}
if (cnt==n){
vector <int> res;
return res;
}
vector <int> res(2);
int start = 0;
int curr_max = 0, global_max = INT_MIN;
for(i=0;i<n;i++){
curr_max += Vec[i];
if (curr_max>global_max && curr_max>=0){
global_max = curr_max;
res[0] = start+1;
res[1] = i+1;
}
else if (curr_max<0){
curr_max = 0;
start = i+1;
}
}
return res;
}