[InterviewBit] Flip

Flip

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector<int> Solution::flip(string A) {
if(count(begin(A), end(A), '1') == A.size()) return {};
int now = 0, ma = 0, map = 0, pick = 0, n = A.size();
vector<int> res;
for(int i = 0; i < n; i++) {
if(A[i] == '1') now += 1;
else now -= 1;
if(now > ma) {
ma = now;
map = i + 1;
}
if(ma - now > pick) {
pick = ma - now;
res = {map + 1, i + 1};
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/21/PS/interviewbit/flip/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.